数据库时,WTForms中的选择验证不会更新 [英] Choices validation in WTForms does not update when database does
问题描述
我知道WTForms中的 SelectField
方法可以参数选择
,它的形式是...
$ b $
选项= [(value1,显示值1),(value2,显示值2)]
我需要根据对数据库的调用来填充我的选择。我使用neo4j作为后端,所以我不能使用模型或其他内置的解决方案来填充表单中的数据。
def get_list_of_things():
query ='start n = node:things(*:*)return ID(n),n.display_name;'
t = cypher.execute(cdb ,查询)[0]
for x in t:
x [0] = str(x [0])
x [1] = str(x [1])$ b $ b things = list(tuple(x)for x)t
返回事物
$ b $ class SelectAThing(Form):
thingID = SelectField('Thing name',choices = get_list_of_things() )
运行选项= get_list_of_things()会生成一个有效的选项列表,非常好, 。
但是,即使数据库没有更新,也不会更新事物列表,而且以后我会返回到这个表单。如果我将数据添加到数据库并返回,我仍然可以看到第一个列表。
把它放在课堂上。把它放在视图代码中。
$ p $ @ app.route('/ route')
def routename()
form = SelectAThing()
form.orgid.choices = get_joinable_orgs()
我发现这个棘手,因为我没有意识到,我可以分配形式,像它是一个普通的python对象初始化它在视图中。
I understand the SelectField
method in WTForms takes can argument choices
which has the form...
choices=[("value1", "display of value 1"), ("value2", "display of value 2")]
I need to populate my choices based on a call to the database. I'm using neo4j as my backend, so I can't use modelforms or the other built-in solutions for populating data in a form.
def get_list_of_things():
query = 'start n = node:things("*:*") return ID(n), n.display_name;'
t = cypher.execute(cdb, query)[0]
for x in t:
x[0] = str(x[0])
x[1] = str(x[1])
things = list(tuple(x) for x in t)
return things
class SelectAThing(Form):
thingID = SelectField('Thing name', choices=get_list_of_things())
Running choices = get_list_of_things() does produce a valid list of choices, great, and this basically works.
However, it doesn't ever seem to update the list of things even when the database does and I return to that form later. If I add things to the db and return, I still see the first list of things.
No dummy, just don't put it in the class. Put it in the view code.
@app.route('/route')
def routename()
form = SelectAThing()
form.orgid.choices=get_joinable_orgs()
I found this tricky because I didn't realize I could assign to form like it was a regular python object after initializing it in the view.
这篇关于数据库时,WTForms中的选择验证不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!