使用 Flask 和 Flask-MongoAlchemy 时的 WTForms 语法
我正在使用 MongoDB 测试 Python 框架 Flask 和 Flask-MongoAlchemy(当然)。当我在测试应用程序中构建多个文档时,我希望通过 WTForms 验证表单。
谁能与我分享一个有关如何在 SelectField() 中创建对象引用的示例?
class Parent(db.Document):
title = db.StringField()
description = db.StringField()
class Object(db.Document):
parent = db.DocumentField(Parent)
title = db.StringField()
@app.route('/object/new', methods=['GET', 'POST'])
def new_object():
form = ObjectForm(obj=Object)
form.parent.choices = [(???) for p in Parent.query.all()] #<-- #1 correct syntax I like to understand, '(t._id, t.title)' didn't work.
if form.validate_on_submit():
form.save()
return redirect(url_for('...'))
return ....
class ObjectForm(wtf.Form):
parent = wtf.SelectField(u'Parent') #<-- #2 do I need to add anything special?
任何建议都会很棒!或者链接到在线示例。谢谢!
I am testing out Python framework Flask and Flask-MongoAlchemy with MongoDB (of course). As I'm building multiple documents in my test app, I like to get the forms validated us WTForms.
Can anyone share with me an example on how to create the object references in a SelectField()?
class Parent(db.Document):
title = db.StringField()
description = db.StringField()
class Object(db.Document):
parent = db.DocumentField(Parent)
title = db.StringField()
@app.route('/object/new', methods=['GET', 'POST'])
def new_object():
form = ObjectForm(obj=Object)
form.parent.choices = [(???) for p in Parent.query.all()] #<-- #1 correct syntax I like to understand, '(t._id, t.title)' didn't work.
if form.validate_on_submit():
form.save()
return redirect(url_for('...'))
return ....
class ObjectForm(wtf.Form):
parent = wtf.SelectField(u'Parent') #<-- #2 do I need to add anything special?
Any suggestion would be great! Or link to an online example. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它记录在引用的 SelectField 的 WTForms 文档中为了方便起见,这里:
我不确定
form.parent.choices
语法,但代码如下所示:It's documented in WTForms documentation of the SelectField quoted here for convenience:
I'm not sure about
form.parent.choices
syntax but the code looks like: