how to use QuerySelectField in flask?

核能气质少年 提交于 2019-11-27 23:07:22

You have two problems:

  1. As Sean Vieira pointed out in his answer, the query_factory callback should return a query, not the results.

  2. 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

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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!