0
我有以下views.py代碼。使用DateTimeFields捕獲'NoneType'異常
now_time = datetime.datetime.now()
for r in requests:
hmt = r.date_of_notification - now_time
if hmt <= datetime.timedelta(days = 1):
r.time_action_status = 'staction_day'
else:
r.time_action_status = 'non_staction_day'
有時候,我得到一個錯誤,因爲在請求某些date_of_notification是空的(NoneType):
TypeError: unsupported operand type(s) for -: 'NoneType' and 'datetime.timedelta'
models.py:
date_of_notification = models.DateTimeField(blank=True, null=True)
什麼樣的驗證爲r.date_of_notification
我應該使用,以避免錯誤?