平台:添加具有多重继承的动态字段 [英] Wtforms: adding dynamic fields with multiple inheritance

查看:210
本文介绍了平台:添加具有多重继承的动态字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以像这样创建动态字段: http://wtforms.simplecodes.com/docs/1.0.1/specific_problems.html#dynamic-form-composition



但上面的解决方案很笨拙在我的情况,并需要一个特殊的API,我想避免。我想知道是否有办法让这个工作与多继承?我尝试了以下方法,但它不起作用,我不知道为什么,我认为WTForms应该在给定类结构的工作方式时正确地绑定表单: $ p> >>> class Base(Form):
... def __init __(self,** kwargs):
... setattr(self,'dynamic_boolean',fields.BooleanField('label'))
... super(Base,self).__ init __(** kwargs)
...
>>>类继承(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使用元类来处理实例化时的绑定。这个元类在 Form .__ init __ 之前调用,因此它不可能用于 __ init __ 创建一个绑定的字段。

WTForms的设计方式是为了减少搜索和查找字段所需的工作量类只在第一次实例化表单时发生,在初始请求之后加快应用程序的速度。






或者如果您愿意参加legwork,可以根据 BaseForm 并使用您自己的元类。需要警告的是,BaseForm与Form不同,它纯粹是一种低级别的方式,专为互补库的作者设计,以获得构建类似工具的权限。


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

But the above solution is unwieldy in my case, and requires a special API which I would like to avoid. I am wondering if there is a way to get this working with multiple inheritance? I tried the following and it won't work and I don't know why, I figured that WTForms should bind the forms properly given how the class structure is working:

>>> 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 the dynamic_boolean is unbound. How can I set this up so that the dynamic_boolean field is bound properly?

解决方案

WTForms uses a metaclass to handle binding at instantiation time. This metaclass does its work before Form.__init__ is called, thus making it not possible for something in __init__ to create a field that's bound.

The way WTForms is designed is done so as to reduce the amount of work to be done in searching for and finding field classes to only happen the first time a form is instantiated, speeding up your application after the initial request.


Alternately If you're willing to put in the legwork, it is possible to design something similar to Form that supports this behavior, based on BaseForm and using your own metaclass. Be warned, BaseForm is not the same thing as Form, it's purely a low-level way designed for authors of complementary libraries to get access to build similar tools.

这篇关于平台:添加具有多重继承的动态字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆