您将需要两个不同的datetime
阈值-today_start
和today_end
:
from datetime import datetime, timedelta, time
today = datetime.Now().date()
tomorrow = today + timedelta(1)
today_start = datetime.combine(today, time())
today_end = datetime.combine(tomorrow, time())
今天发生的所有事情都必须在 之前 开始today_end
在 之后 结束today_start
,因此:
class EventManager(models.Manager):
def bookings_today(self, location_id):
# Construction of today_end / today_start as above, omitted for brevity
return self.filter(location=location_id, start__lte=today_end, end__gte=today_start)
(PS叫DateTimeField
(非DateField
)foo_date
会令人误解,请考虑-start
和end
…)