Not a Valid Choice for Dynamic Select Field WTFORMS
My guess is that Area.id
is a int
- when data comes back from the client it is treated as a string by WTForms unless a callable is passed to the coerce
keyword argument of the wtforms.fields.SelectField
constructor:
area = SelectField(coerce=int)
Alternately, if you are using SQLAlchemy you could use wtforms.ext.sqlalchemy.fields.QuerySelectField
(wtforms_sqlalchemy
if you are using WTForms 3+):
area = QuerySelectField(query_factory=Area.objects.all, get_pk=lambda a: a.id, get_label=lambda a: a.name)
Remember to put also the available choices when setting up the Form class.
class MyForm(FlaskForm): sel = SelectField(coerce=int, label="MyLabel", choices=my_choices) ....
where choices can be something like this:
my_choices = [x.id for x in get_products()]
The exception is arised in function pre_validate
in script /wtforms/fields/core.py
when the validate_on_submit()
functions is called