[Django]-How to filter objects on datetime fields?

4👍

You need to use queryset filter lookups, __gte (greater than or equal):

announcements = Announcement.objects.all().filter(date_start__gte = datetime.now())

3👍

This syntax is wrong:

announcements = Announcement.objects.all().filter(date_start >= datetime.now())

filter requires Python keyword arguments. So in effect you could:

announcements = Announcement.objects.all().filter(date_start__gte=datetime.now())

More on that on Django docs: Making queries: Field lookups.

👤Wtower

Leave a comment