Wtforms: adding dynamic multiple-inheritance fields

I know that I can create dynamic fields as follows: http://wtforms.simplecodes.com/docs/1.0.1/specific_problems.html#dynamic-form-composition

But the above solution is cumbersome in my case and requires a special API that I would like to avoid. I am wondering if there is a way to make this work with multiple inheritance? I tried the following and it won’t work, and I don’t know why, I realized that WTForms should bind the forms correctly set how the class structure works:

>>> 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}

As you can see, dynamic_boolean is not connected. How can I set this so that the dynamic_boolean field is properly bound?

+4
1

WTForms . , Form.__init__, - __init__ .

WTForms , , , , , .


. , Form, , BaseForm . , BaseForm - , Form, , , .

+4

Source: https://habr.com/ru/post/1540105/


All Articles