给定以下两种 WTF 形式:
class NestedForm(FlaskForm):
note = StringField('Note', validators=[DataRequired()])
some_id = StringField('Some ID', validators=[DataRequired()])
class Form(FlaskForm):
id = HiddenField('ID')
nested_forms = FieldList(
FormField(NestedForm),
min_entries=1,
)
如何动态设置some_id
的值?例如,在 Flask View 中,如果尚未提供 some_id
的值。
我尝试使用以下方法设置值:
form.nested_forms[0].some_id = "some_contextual_default_value"
form.nested_form[0].data['some_id'] = "some_contextual_default_value"
...这似乎没有做任何事情。表单验证继续失败,并出现错误,指出缺少必填字段 (some_id
)。
最佳答案
使用data
属性。
form.nested_forms[0].some_id.data = "some_contextual_default_value"
关于python-3.x - 如何动态设置 WTForms FormField 的默认值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60535585/