动态表单的UnboundField对象错误

时间:2018-01-26 17:23:15

标签: python flask

我有一个动态表格:

class CollectionRequestParamForm(Form):
    param_human_name = StringField(validators=[DataRequired()])
    param_request_name = StringField()


class CollectionRequestParamCombinedForm(FlaskForm):
    params = FieldList(FormField(CollectionRequestParamForm), min_entries=2)
    submit = SubmitField('Submit')

我想在视图中设置min_entries,例如:

collection_request_params_form = CollectionRequestParamCombinedForm(min_entries=5)

我这样做:

class CollectionRequestParamCombinedForm(FlaskForm):
    def __init__(self, min_entries, *args, **kwargs):
        FlaskForm.__init__(self, *args, **kwargs)
        self.params = FieldList(FormField(CollectionRequestParamForm), min_entries=min_entries)

    submit = SubmitField('Submit')

但是:TypeError: 'UnboundField' object is not iterable

导致此错误的原因是什么?

1 个答案:

答案 0 :(得分:0)

如果要使其动态化,请执行此操作

for i in range(min_entries):
    form.params.append_entry()

更多信息,https://stackoverflow.com/a/48710958/9218468