Python多类层次结构\uuu init \uuuuuu未执行

2024-06-16 11:29:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是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类


Tags: djangoselfinitdefargsmethodkwargsclass