我使用的是django,但这是一个比较通用的python问题。你知道吗
我已经定义了一个类,我打算用它来扩展ModelForm和Form类django.表格. 你知道吗
代码如下所示:
class FormMixin(object):
def __init__(self, *args, **kwargs):
""" every method ocurrence must call super """
super(FormMixin, self).__init__(*args, **kwargs)
self.new_attr = 'This is an attribute'
class ModelFormAdapter(forms.ModelForm):
""" I use this class so __init__ signatures match """
def __init__(self, *args, **kwargs):
""" every method ocurrence must call super """
super(ModelFormAdapter, self).__init__(*args, **kwargs)
class BaseModelForm(ModelFormAdapter, FormMixin):
def __init__(self, *args, **kwargs):
""" BaseModelForm never gets the attribute new_attr """
super(BaseModelForm, self).__init__(*args, **kwargs)
我甚至调试过它,而且FormMixininit方法从未被调用。我做错什么了?我想要实现的是向表单添加一些属性,并预处理字段标签和css类
这是因为
ModelFormAdapter
的一个祖先(BaseForm
)不调用super
,链就断了。将FormMixin
放在父列表的第一位。你知道吗相关问题 更多 >
编程相关推荐