当我创建CustomerForm()是EnhancedModelForm()的子类时,默认的clean方法不会考虑使用__init__()
初始化表单时所做的更改(required = True)?这是为什么?
class EnhancedModelForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(EnhancedModelForm, self).__init__(*args, **kwargs)
test = self.errors
表格
class CustomerForm(EnhancedModelForm):
class Meta:
model = Customer
fields = ('salutation', 'first_name', 'last_name', 'phone_number', 'email_address')
def __init__(self, *args, **kwargs):
super(CustomerForm, self).__init__(*args, **kwargs)
self.fields['phone_number'].required = True
self.fields['email_address'].required = True
答案 0 :(得分:0)
我现在使用django.forms.form.BaseForm()
中的_post_clean()挂钩,而不是覆盖 init ()方法。"""
An internal hook for performing additional cleaning after form cleaning
is complete. Used for model validation in model forms.
"""
这很棒