我想使用从路由传入的默认值来查询选择字段。我不知道如何将变量从 View 传递到 Form 类
class transactionsForm(Form):
loan_id = QuerySelectField('trans_id', validators=[Required()], get_label='name',
query_factory=lambda: trans.query.filter_by(trans_id=[MY VARIABLE]).all())
请您参考如下方法:
这是来自 QuerySelectField文档:
The query property on the field can be set from within a view to assign a query per-instance to the field. If the property is not set, the query_factory callable passed to the field constructor will be called to obtain a query.
这意味着您使用查询定义表单:
class transactionsForm(Form):
loan_id = QuerySelectField('trans_id', validators=[Required()], get_label='name')
然后在你的 View 函数中,一旦你有一个实例,你就分配查询:
def viewFunction(my_variable):
form = transactionsForm()
my_query = trans.query.filter_by(trans_id=my_variable)
form.loan_id.query = my_query
if form.validate_on_submit():
# ...
