[英]Python WTForms: How to dynamically set options of a SelectField in FieldList of FormFields?
我目前正在尝试使用WTForms
的FieldList
和FormField
附件,以允许用户添加具有相应覆盖范围的位置的自定义子集。
该表单向用户提供一些标准输入,然后使用一组字段(FormField)进行初始化,每个字段都包含一个位置选择输入和一个覆盖量输入。
Javascript允许用户根据需要添加或删除其他位置覆盖字段集,以反映准确的文档信息。
问题:作为一种解决方法,我通过在表单处理程序的GET请求中传递模板变量并手动创建自己的表单字段来设置位置选项。 但是,这并没有更新实际的WTForms位置字段选择,因此,当我提交表单时,会为位置字段引发一个异常(“无效选择”)。
实例化MyForm
时,如何动态地将位置选择添加到LocationForm的location
字段中?
这基本上就是我的代码的样子:
注意:由于这不是理想的设计,因此我省略了在GET请求中创建位置模板变量的代码。 我想更符合预期的WTForms方法 :
class LocationForm(Form):
location = SelectField('Location', [], choices=[])
coverage = FloatField('Coverage', [])
class MyForm(BaseForm):
# other fields omitted for brevity
location_coverage = FieldList(FormField(LocationForm), [], min_entries=1)
class AddDocument(BaseHandler):
def get(self):
params = {
"cid": cid
}
return self.render_template("form.html", **params)
def post(self):
cid = self.request.get('cid')
if not self.form.validate():
return self.get()
company_key = ndb.Key('Company', cid)
doc = Document(parent=company_key)
self.form.populate_obj(doc)
doc.put()
params = {
"cid":
}
return self.redirect_to('view_company', **params)
@webapp2.cached_property
def form(self):
f = MyForm(self)
# HERE is where I would normally do something like:
# company = ndb.Key('Company', int(self.request.get('cid')))
# locations = ndb.Location.query(ancestor=company).fetch()
# f.field_name.choices = [(loc.key, loc.name) for loc in locations]
# but this doesn't work with Select Fields enclosed in
# FormFields and FieldLists.
return f
编辑:
我创建了一个解决方案,但这不是我想要的答案。 就我而言,我只是将LocationForm.location
表单字段从SelectField更改为StringField。 这样做绕过了选择字段选择的验证,并允许表单提交。 这不是理想的设计,因为它不是预期的设计,但是如果有人可以引导我寻求在这种特定情况下使用WTForms的更正确方法,我将不胜感激。
如果您的BaseForm类从实例化后的发布数据中填充表单,则应该看到通常在将选择直接添加到表单的SelectField的位置处填充的嵌套表单。
因此类似:
for entry in f.location_coverage.entries:
entry.location.choices = [(loc.key, loc.name) for loc in locations]
应该将选择填充到每个子表单选择字段中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.