【发布时间】:2016-12-21 10:53:39
【问题描述】:
我的 django 应用程序 live_from 和 live_to 中有字段,这些字段不是必需的。当此字段为空时,我的方法中出现错误:
字段:
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())
错误:TypeError: unorderable types: NoneType() <= datetime.datetime()
【问题讨论】:
-
看起来不像是编码错误,更像是设计错误。如果这些字段为空,
is_live应该做什么? -
所以
live_from或live_to都是None,因为您允许空值。life_from的例外情况在这里是None,但同样适用于live_to。如果其中任何一个为空,会发生什么?