kvesteri / wtforms-alchemy

Tools for creating wtforms from sqlalchemy models
Other
245 stars 59 forks source link

no support for ARRAY column type #97

Closed havok2063 closed 8 years ago

havok2063 commented 8 years ago

I'm trying to use WTForms-Alchemy to map my SQLAlchemy classes from a PostgreSQL db into forms. I'm getting errors when trying to convert some basic declarative base classes. Namely, the array column type is not recognized.

class CubeForm(ModelForm):
    class Meta:
        model = datadb.Cube
   ....:

UnknownTypeException: Unknown type 'FLOAT[]' for column 'specres' Can we add support for this column type? This would greatly help as I have many array columns.

kvesteri commented 8 years ago

Sure, what should be the default field type an ARRAY should convert itself into?

havok2063 commented 8 years ago

That I'm not sure. All of my arrays are either of type Floats, Numerics, or Integers. I'm not sure what the appropriate form field should look like. How should one be able to search through an array? I imagine it would be searching for a subset of values within the array, either as a range between two values or as a <, > some value. I don't know how that would translate. A TextField maybe?

Or at the very least, I'd like to still be able to build the ModelForm class without it raising an exception when it encounters a type it doesn't understand. As long as the column still shows up, and can be 'selected' on, them I'm ok not having a field associated with it, if it's too difficult.

For my usage, no one will be editing the values in the database. I want to use WTForms as an easy means of selecting what parameters to search on, and using what values.

havok2063 commented 8 years ago

Any status update on this? @kvesteri Do you need any more info from me, or is there anything I can to help?

kvesteri commented 8 years ago

You can either add your desired type conversion to the FormGenerator.TYPE_MAP or you can just explicitly add these fields directly as TextFields (I would recommend this option).

class MySearchForm(ModelForm):
    class Meta:
         pass
    my_array_field = TextField()
havok2063 commented 8 years ago

Thanks for the feedback. The problem with the explicit definition is that I am generating all of my forms dynamically, and automatically. So I'm a bit hesitant to start hard-coding things. In lieu of a better solution, I implemented the former option, and added these lines into FormGenerator

from sqlalchemy.dialects import postgresql

and inside the TYPE_MAP

(postgresql.ARRAY, DecimalField),
(postgresql.JSON, DecimalField),

SQLAlchemy has a general ARRAY type, however, it is not present when I look at sa.types, so I imported the postgresql specific version. I'm not sure if this is generic enough to do a pull request, but it would be nice to integrate this back into the pip-installable global version, rather than my own.

kvesteri commented 8 years ago

I think this is not generic enough. It really depends on situation how one would want to render an ARRAY field. Glad you found a good solution for your specific case.