0
votes

In a nutshell, I am making a Flask-wtf application in which I have a select field as:

someVar=SelectField("someVar",choices=result)

where result is a list of data fetched from the DB.

Now the problem is on front end result showing values like ('abcxyz',) instead of just "abcxyz" whereas the DB query is also producing the value as simply "abcxyz". Also I want the dropdown as blank on page load.

I am showing this select field value on html as :

{{ form.someVar.label }}{{ form.someVar}}
2

2 Answers

0
votes

I suggest you use QuerySelectField instead of SelectField, since you're fetching data from the DB to populate the dropdown.

from wtforms.ext.sqlalchemy.fields import QuerySelectField

So you'd do something like,

someVar = QuerySelectField("someVar",
                        blank_text='select someVar',
                        query_factory=lambda: Model.query.all(),
                        allow_blank=True)

Here you use query_factory to set the choices. Also note the blank_text attribute will make sure that no value is selected by default.

0
votes

The return value is a tuple. Instead of "someVar=SelectField("someVar",choices=result)" use "someVar=SelectField("someVar",choices=result[0])"