gpt4 book ai didi

python - 将变量传递给 Flask WTForm

转载 作者:太空宇宙 更新时间:2023-11-04 07:21:13 25 4
gpt4 key购买 nike

我想使用从路由传入的默认值来查询选择字段。我不知道如何将变量从 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():
# ...

关于python - 将变量传递给 Flask WTForm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17980432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com