Django filter events occurring today

You’ll need two distinct datetime thresholds – today_start and 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())

Anything happening today must have started before today_end and ended after today_start, so:

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)

(P.S. Having a DateTimeField (not a DateField) called foo_date is irritatingly misleading – consider just start and end…)

Leave a Comment