ModelForm class 的双重继承,无法更改必填字段
double inheritence of ModelForm class, can't change required fields
我有一个 ModelForm class,我将其用作所有其他表单 class 的父级。它看起来像这样:
class BootstrapForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(BootstrapForm, self).__init__(*args, **kwargs)
for field in self.fields:
self.fields[field].widget.attrs['class'] = 'form-control text-center'
if field != 'email':
self.fields[field].widget.attrs['class'] += ' text-capitalize'
for error in self.errors:
if error in self.fields:
self.fields[error].widget.attrs['class'] += ' is-invalid'
我还有一个模型,它有一个字段 'tax_no',空白设置为 True:
class Institution(CustomModel):
name = models.CharField(max_length=256,
null=False,
blank=False,
verbose_name=_('nazwa'))
address = models.ForeignKey('Address',
on_delete=models.PROTECT,
null=False,
blank=False,
verbose_name=_('adres'))
phone = PhoneNumberField(null=True,
blank=True,
verbose_name=_('telefon'))
tax_no = models.CharField(max_length=15,
null=True,
blank=True,
validators=[validate_tax_number, ],
verbose_name=_('NIP'))
我想要的是一个默认允许空 tax_no 字段的模型,但我希望可以根据需要将其设置为必需字段。
我的问题是,当我尝试像这样将字段设置为必需时:
class InvoiceInstitutionForm(BootstrapForm):
class Meta:
model = Institution
exclude = ('address',)
def __init__(self,*args, **kwargs):
super(InvoiceInstitutionForm, self).__init__(*args,**kwargs)
print(self.fields['tax_no'].required)
self.fields['tax_no'].required = True
它只有在 InvoiceInstitutionForm 直接继承自 forms.ModelForm 时才能正常工作。从 BootsrapForm class 继承时,它不起作用。
奇怪的是它正确地生成了 html 字段:
<input type="text" name="invoice-tax_no" maxlength="15" class="form-control text-center text-capitalize" required="" id="id_invoice-tax_no">
但是验证的时候不关心这个。即使 tax_no 为空,它也可以有效。
有什么想法为什么它不起作用以及如何解决它吗?
当您执行 for error in self.errors:
时,访问 self.errors
会导致验证表单。当您调用 super(InvoiceInstitutionForm, self).__init__(*args,**kwargs)
.
时会发生这种情况
当您在下一行设置 required = True
时,为时已晚,因为表单已经过验证。
我最初建议您可以使用 error_css_class
选项而不是循环 self.errors
,但正如您在评论中所说,只有在您使用 {{ form.as_p }}
时才有效, {{ form.as_table }}
.
我有一个 ModelForm class,我将其用作所有其他表单 class 的父级。它看起来像这样:
class BootstrapForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(BootstrapForm, self).__init__(*args, **kwargs)
for field in self.fields:
self.fields[field].widget.attrs['class'] = 'form-control text-center'
if field != 'email':
self.fields[field].widget.attrs['class'] += ' text-capitalize'
for error in self.errors:
if error in self.fields:
self.fields[error].widget.attrs['class'] += ' is-invalid'
我还有一个模型,它有一个字段 'tax_no',空白设置为 True:
class Institution(CustomModel):
name = models.CharField(max_length=256,
null=False,
blank=False,
verbose_name=_('nazwa'))
address = models.ForeignKey('Address',
on_delete=models.PROTECT,
null=False,
blank=False,
verbose_name=_('adres'))
phone = PhoneNumberField(null=True,
blank=True,
verbose_name=_('telefon'))
tax_no = models.CharField(max_length=15,
null=True,
blank=True,
validators=[validate_tax_number, ],
verbose_name=_('NIP'))
我想要的是一个默认允许空 tax_no 字段的模型,但我希望可以根据需要将其设置为必需字段。 我的问题是,当我尝试像这样将字段设置为必需时:
class InvoiceInstitutionForm(BootstrapForm):
class Meta:
model = Institution
exclude = ('address',)
def __init__(self,*args, **kwargs):
super(InvoiceInstitutionForm, self).__init__(*args,**kwargs)
print(self.fields['tax_no'].required)
self.fields['tax_no'].required = True
它只有在 InvoiceInstitutionForm 直接继承自 forms.ModelForm 时才能正常工作。从 BootsrapForm class 继承时,它不起作用。 奇怪的是它正确地生成了 html 字段:
<input type="text" name="invoice-tax_no" maxlength="15" class="form-control text-center text-capitalize" required="" id="id_invoice-tax_no">
但是验证的时候不关心这个。即使 tax_no 为空,它也可以有效。 有什么想法为什么它不起作用以及如何解决它吗?
当您执行 for error in self.errors:
时,访问 self.errors
会导致验证表单。当您调用 super(InvoiceInstitutionForm, self).__init__(*args,**kwargs)
.
当您在下一行设置 required = True
时,为时已晚,因为表单已经过验证。
我最初建议您可以使用 error_css_class
选项而不是循环 self.errors
,但正如您在评论中所说,只有在您使用 {{ form.as_p }}
时才有效, {{ form.as_table }}
.