我想添加一個自定義的驗證作爲我的窗體的一部分。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