我在 Django 应用程序中有一个看起来像这样的模型
class Flight(models.Model):
code = models.CharField(max_length = 15, primary_key = True)
airline = models.ForeignKey(Airline)
departure_airport = models.ForeignKey(Airport, related_name='flight_departure')
departure_time = models.TimeField()
arriving_airport = models.ForeignKey(Airport, related_name='flight_arriving')
arriving_time = models.TimeField()
aircraft = models.ForeignKey(Aircraft)
我现在只想检索在当前时间之后起飞的航班。为此,我在控制台上尝试了以下操作:
from datetime import *
time = datetime.time(datetime.now())
f = Flight.objects.filter(departure_time > time)
运行前面的语句后,我收到以下错误:
Traceback (most recent call last):
File "<console>", line 1, in <module>
NameError: name 'departure_time' is not defined
另外,如果我尝试以下操作,我也会收到类似的错误:
f = Flight.objects.filter(departure_time > '12:00:00')
但是,这有效:
f = Flight.objects.filter(departure_time = '12:00:00')
print f
[<Flight: LH17>]
如果有人可以就如何在 TimeField 字段上比较(查询)时间提供建议,我将非常感激?
谢谢。