我知道我可以像这样创建动态字段:http://wtforms.simplecodes.com/docs/1.0.1/specific_problems.html#dynamic-form-composition
但是在我的情况下,上述解决方案很笨重,并且需要一个特殊的API,我想避免使用。我想知道是否有一种方法可以通过多重继承使其工作?我尝试了以下代码,但它不起作用,而我不知道原因,基于类结构的工作方式,我认为WTForms应该正确地绑定表单:
>>> class Base(Form):
... def __init__(self, **kwargs):
... setattr(self, 'dynamic_boolean', fields.BooleanField('label'))
... super(Base, self).__init__(**kwargs)
...
>>> class Inherit(Base):
... other_boolean = fields.BooleanField('label')
...
>>>
>>> form = Inherit()
>>> form.__dict__
{'dynamic_boolean': <UnboundField(BooleanField, ('label',), {})>, 'other_boolean': <wtforms.fields.core.BooleanField object at 0x8a8510c>, '_fields': {'other_boolean': <wtforms.fields.core.BooleanField object at 0x8a8510c>}, '_prefix': '', '_errors': None}
从上面的代码中我们可以看到,dynamic_boolean没有绑定。那么我该如何设置才能使dynamic_boolean字段正确地绑定呢?