1

我尝试从一个模型中选择所有对象,该模型的日期时间字段设置为从现在起不到 12 小时。

我的查看代码:

now = datetime.datetime.now()

# selects all events with date_time field set in future, and with reminder field active
events = Event.objects.filter(reminder=True, reminder_sent=False, date_time__gt=now)

# sets timedelta, as we want to filter previously selected objects to be max 12 hours in future from now
delta = now + datetime.timedelta(hours=12)

# returns nothing, even if in database there are Event objects with date_time field set to 1 hour from now 
expiring_events = events.filter(date_time__lt=delta)
4

1 回答 1

4
now = datetime.datetime.now()
delta = now + datetime.timedelta(hours=12)
events = Event.objects.filter(reminder=True, reminder_sent=False, date_time__range=(now, delta))
于 2013-07-01T11:54:15.537 回答