我想添加一个自定义的验证作为我的窗体的一部分。Django - 自定义验证错误
我试图在voluntary_date_display_type是指定的数字时跳过自定义验证。但是,当我运行以下代码时,voluntary_date_display_type值为无,我期待的是数字/数字。
我已阅读form field validation上的django文档,但我看不到我的错误。
当前只有最后的else条件被触发,因为该值为None。
有人能指出我做错了什么吗?
这里是我的forms.py文件中的代码:
class Meta:
model = VoluntaryDetails
fields = (
.......
'voluntary_date_display_type',
.......
)
def clean_voluntary_finish_date(self):
voluntary_display_type = self.cleaned_data.get('voluntary_display_type')
voluntary_start_date = self.cleaned_data.get('voluntary_start_date')
voluntary_finish_date = self.cleaned_data.get('voluntary_finish_date')
voluntary_date_display_type = self.cleaned_data.get('voluntary_date_display_type')
if voluntary_display_type == 0:
if voluntary_finish_date is not None and voluntary_start_date is not None:
if voluntary_start_date > voluntary_finish_date:
if voluntary_date_display_type == 2 or voluntary_date_display_type == 3:
raise forms.ValidationError(_("To Date must be after the From Date."))
elif voluntary_date_display_type == 4 or voluntary_date_display_type == 5:
raise forms.ValidationError(_("Finish Date must be after the Start Date."))
elif voluntary_date_display_type == 6 or voluntary_date_display_type == 7:
raise forms.ValidationError(_("End Date must be after the Begin Date."))
elif voluntary_date_display_type == 8:
raise forms.ValidationError(_("This Date must be after the other Date."))
elif voluntary_date_display_type == 9 or voluntary_date_display_type == 10:
raise forms.ValidationError(_("This Duration date must be after the other Duration date."))
else:
raise forms.ValidationError(_("Completion Date must be after the Commencement Date."))
return voluntary_finish_date