I am trying to have a select field filled with the results of a sqlalchemy request in a flask form.
Here are the code :
def possible_book():
retu
One possible problem is that wtforms.ext.sqlalchemy.fields.QuerySelectField expects a SQLAlchemy query object, not a materialized list. Simply remove the all()
call from your possible_book
return to return an unmaterialized query to WTForms:
def possible_book():
return Book.query.with_entities(Book.id)
You have two problems:
As Sean Vieira pointed out in his answer, the query_factory
callback should return a query, not the results.
The query_factory
callback should return complete entities, in your case books, not book ids. I believe the QuerySelectField
must be trying to use the results of the query as if they are mapped objects but the ids (returned as KeyedTuple instances) are not.
I think this is the correct way to code the callback function:
def possible_book():
return Book.query