我正在尝试django custom validate表单字段。
我已经编写了以下代码,但我想不出如何引用EmploymentRecordForm中包含的 employment_record_date_display_type 的提交值。
我尝试了 employment_record_date_display_type = EmploymentRecordForm.employment_record_date_display_type ,但却出现以下错误:
类型对象'EmploymentRecordForm'没有属性'employment_record_date_display_type'
以下是我的forms.py文件中的验证码:
class EmploymentRecordForm(forms.ModelForm):
.............
def clean_employment_record_completion_date(self):
employment_record_display_type = self.cleaned_data.get('employment_record_display_type')
employment_record_date_display_type = ?????????
employment_record_commencement_date = self.cleaned_data.get('employment_record_commencement_date')
employment_record_completion_date = self.cleaned_data.get('employment_record_completion_date')
if employment_record_display_type == 0:
if employment_record_completion_date is not None and employment_record_commencement_date is not None:
if employment_record_commencement_date > employment_record_completion_date:
if employment_record_date_display_type == 2 or employment_record_date_display_type == 3:
raise forms.ValidationError(_("To Date must be greater than the From Date."))
elif employment_record_date_display_type == 4 or employment_record_date_display_type == 5:
raise forms.ValidationError(_("Finish Date must be greater than the Start Date."))
elif employment_record_date_display_type == 6 or employment_record_date_display_type == 7:
raise forms.ValidationError(_("End Date must be greater than the Begin Date."))
elif employment_record_date_display_type == 8 or employment_record_date_display_type == 9:
raise forms.ValidationError(_("This Date must be greater than the other Date."))
else:
raise forms.ValidationError(_("Completion Date must be after the Commencement Date."))
return employment_record_completion_date
如何引用jobs_record_date_display_type的提交值?
答案 0 :(得分:0)
您应该编写自定义验证程序类,并在您希望应该具有验证程序列表属性的字段中。像这样:
price = models.IntegerField(validators=[MyCustomValidator])