Python TypeError:无序类型:NoneType()<;=datetime.datetime()
我的django应用程序中有来自的Python TypeError:无序类型:NoneType()<;=datetime.datetime(),python,django,datetime,Python,Django,Datetime,我的django应用程序中有来自的live_和live_到的字段,这些字段不是必需的。当此字段为空时,我的metod中出现错误: 字段: live_from = models.DateTimeField('live from', blank=True, null=True) live_to = models.DateTimeField('live to', blank=True, null=True) 以下是我的方法: def is_live(self): return (self.
live_和live_到
的字段,这些字段不是必需的。当此字段为空时,我的metod中出现错误:
字段:
live_from = models.DateTimeField('live from', blank=True, null=True)
live_to = models.DateTimeField('live to', blank=True, null=True)
以下是我的方法:
def is_live(self):
return (self.live_from <= timezone.now()) and (self.live_to >= timezone.now())
def处于活动状态(自身):
return(self.live_from=timezone.now())
错误:TypeError:unorderable types:NoneType()我认为您试图与当前时间进行比较,当值最初为空时,应该返回False,例如:
def is_live(self):
if self.live_from is None or self.live_to is None :
return False
return (self.live_from <= timezone.now()) and (self.live_to >= timezone.now())
def处于活动状态(自身):
如果self.live\u from为无或self.live\u to为无:
返回错误
return(self.live_from=timezone.now())
我认为您试图与当前时间进行比较,当值最初为空时,应返回False,例如:
def is_live(self):
if self.live_from is None or self.live_to is None :
return False
return (self.live_from <= timezone.now()) and (self.live_to >= timezone.now())
def处于活动状态(自身):
如果self.live\u from为无或self.live\u to为无:
返回错误
return(self.live_from=timezone.now())
考虑到您的型号,这将是一个很好的定义
def is_live(self):
# first, check the inexpensive precondition, before comparing date fields
return ((None not in [self.live_from, self.live_to]) and
self.live_from <= timezone.now() and self.live_to >= timezone.now())
def处于活动状态(自身):
#首先,在比较日期字段之前,检查前提条件
返回((不在[self.live\u from,self.live\u to]中)和
self.live\u from=timezone.now())
考虑到您的型号,这将是一个很好的定义
def is_live(self):
# first, check the inexpensive precondition, before comparing date fields
return ((None not in [self.live_from, self.live_to]) and
self.live_from <= timezone.now() and self.live_to >= timezone.now())
def处于活动状态(自身):
#首先,在比较日期字段之前,检查前提条件
返回((不在[self.live\u from,self.live\u to]中)和
self.live\u from=timezone.now())
看起来不像是编码错误,更像是设计错误。如果这些字段是空的,那么应该执行什么操作?因此live\u from
或live\u to
都是无的,因为您允许空值。例外情况是,这里的life\u from
是None
,但live\u to
也是如此。如果其中一个为空,会发生什么?看起来不像是编码错误,更像是设计错误。如果这些字段是空的,那么应该执行什么操作?因此live\u from
或live\u to
都是无的,因为您允许空值。例外情况是,这里的life\u from
是None
,但live\u to
也是如此。如果其中一个是空的,会发生什么?