我有一个模型,当用户在date_fields中放置回溯日期或输入到今天的较早日期时,我想引发错误。
class Leave_Management(models.Model):
employee = models.ForeignKey('employeeModel', on_delete=models.CASCADE)
name = models.CharField(max_length=50)
reason = models.TextField(max_length=200)
date_to = models.DateField(null=True)
date_from = models.DateField(null=True)
表格
class LeaveForm(forms.ModelForm):
class Meta:
model = Leave_Management
fields = ('__all__')
def clean_date_to(self):
date_to = self.cleaned_data.get('date_to')
if not date_to > datetime.date.today():
raise ValidationError('Enter Valid Date')
return date_to
我尝试编写此验证代码,但显示错误 TypeError at /home/leave
'>' not supported between instances of 'NoneType' and 'datetime.date'
print(date_to)仅显示date 2018-06-2
print(datetime.date.today()) 像这样显示 datetime.date(2018, 6, 20)
我究竟做错了什么?
BIG阳
相关分类