【发布时间】:2014-05-21 07:46:24
【问题描述】:
我知道我可以创建这样的动态字段: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 字段?
【问题讨论】:
-
您的主题似乎与您的问题不符。 WTForms确实支持多重继承,但这与你想要做的事情无关,这类似于动态组合。