用类扩展Django ModelForm

时间:2015-02-24 19:48:20

标签: python django inheritance django-models django-forms

我的Django项目中有多个模型表单,并希望为所有表单添加三个字段。

但是当我编写一个简单的表单然后将其子类化时,我会收到错误。

class PersonForm(forms.Form):
    person_name = forms.CharField(
        label=u'Entregado por',
        max_length=64,
    )
    person_document_number = forms.CharField(
        label=u'Número de Documento',
        max_length=15,
    )
    person_document_type = forms.ChoiceField(
        label=u'Tipo de Documento',
        choices=BLANK_CHOICE_DASH + list(DOCUMENT_TYPE_CHOICES),
    )

class ContractForm(PersonForm, forms.ModelForm):
    class Meta:
        model = Contract

    def __init__(self, *args, **kwargs):
        super(ContractForm, self).__init__(*args, **kwargs)
        self.fields['person_name'].widget.attrs['class'] = 'js-person-name'

错误:

TypeError: Error when calling the metaclass bases
    metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases

我尝试定义PersonForm之类的

class PersonForm:
    person_name = forms.CharField(
        label=u'Entregado por',
        max_length=64,
    )
    # ....

没有从forms.Form继承,并收到错误:

django.core.exceptions.FieldError: Unknown field(s) (person_document_type, person_document_number, person_name) specified for Contract

有没有漂亮的方法(不将这些字段复制到我的所有模型表格中)?

1 个答案:

答案 0 :(得分:3)

为什么不用ModelForm实现PersonForm?

class PersonForm(forms.ModelForm):
    person_name = forms.CharField(
        label=u'Entregado por',
        max_length=64,
    )
    person_document_number = forms.CharField(
        label=u'Número de Documento',
        max_length=15,
    )
    person_document_type = forms.ChoiceField(
        label=u'Tipo de Documento',
        choices=BLANK_CHOICE_DASH + list(DOCUMENT_TYPE_CHOICES),
    )

class ContractForm(PersonForm):
    class Meta:
        model = Contract

    def __init__(self, *args, **kwargs):
        super(ContractForm, self).__init__(*args, **kwargs)
        self.fields['person_name'].widget.attrs['class'] = 'js-person-name'