我有一个模型,当用户在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-2print(datetime.date.today()) 像这样显示 datetime.date(2018, 6, 20)我究竟做错了什么?
2 回答

BIG阳
TA贡献1859条经验 获得超6个赞
我已经按照@Satendra的指示解决了这个问题,我将(date_to<datetime.date.get('date_to))
其保留在括号中,并且工作正常。
添加回答
举报
0/150
提交
取消