【发布时间】:2012-11-26 17:24:09
【问题描述】:
我了解 WTForms 中的 SelectField 方法采用可以参数 choices 的形式...
choices=[("value1", "display of value 1"), ("value2", "display of value 2")]
我需要根据对数据库的调用来填充我的选择。我使用 neo4j 作为后端,所以我不能使用模型表单或其他内置解决方案来填充表单中的数据。
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())
运行选项 = get_list_of_things() 确实会生成一个有效的选项列表,很好,这基本上可以工作。
然而,它似乎永远不会更新事物列表,即使数据库更新了,我稍后会返回该表单。如果我将东西添加到数据库并返回,我仍然会看到第一个东西列表。
【问题讨论】:
标签: python flask wtforms py2neo