我在Django中有一个具有两个 DateField 的模型,但有时它们从前端接收到错误的日期,例如“ 20196-10-23”。即使实际上可能是一个有效的日期(疯狂但合法),由于以下错误,python也不允许比较两个日期:TypeError: '>=' not supported between instances of 'datetime.date' and 'str'
,所以我想使用clean()
方法来验证这两个日期都是有效的,但是即使日期正确,我也总是会弄错日期。
这是clean()
方法的代码:
def clean(self, *args, **kwargs):
try:
Reservation.objects.get(booking=self.booking)
except:
pass
else:
raise CustomValidation(_('Booking already exists.'), 'booking', status_code=status.HTTP_400_BAD_REQUEST)
print("{}/{}".format(self.arrival_date, self.departure_date))
try:
datetime.strptime(self.arrival_date, "%Y-%m-%d")
except:
raise CustomValidation(_('Arrival date must be a valid date.'), 'arrival_date', status_code=status.HTTP_400_BAD_REQUEST)
if self.arrival_date >= self.departure_date:
raise CustomValidation(_('Departure date must be later than Arrival date.'), 'departure_date', status_code=status.HTTP_400_BAD_REQUEST)
elif self.arrival_date <= timezone.datetime.now().date():
if self.id == None:
raise CustomValidation(_('Arrival date must be later than today.'), 'arrival_date', status_code=status.HTTP_400_BAD_REQUEST)
if self.status == 'CONFIRMED' and self.confirmation is None:
raise CustomValidation(_('Must provide a confirmation number.'), 'confirmation', status_code=status.HTTP_400_BAD_REQUEST)
即使日期正确,我也总是例外。
答案 0 :(得分:0)
虽然从概念上讲20196是有效年份,但Python不允许这样做。我试过了...
import datetime
from datetime import date
def clean(arrival_date):
return date(*map(int, arrival_date.split("-")))
print(clean("20196-10-23"))
# ValueError: year is out of range
...,发现实际上存在用于Python日期的硬编码maximum year value of 9999。
因此,尽管您可以通过各种方式从技术上验证格式,但无法使用内置的datetime
模块来处理此类日期。
答案 1 :(得分:0)
我通常首先将两个日期都设置为datetime.strptime()
:
try:
#get the dates
my_date_time_1 = self.cleaned_data['date_1']
my_date_time_2 = self.cleaned_data['date_2']
#set the datetime
my_date_time_1 = datetime.strptime(my_date_time_1, '%Y-%m-%d %H:%M:%S')
my_date_time_2 = datetime.strptime(my_date_time_2, '%Y-%m-%d %H:%M:%S')
except:
raise forms.ValidationError(u'Wrong Date Format')
#and then compare dates
if my_date_time_1 >= my_date_time_2:
raise forms.ValidationError(u'Please check the dates')