Django, queryset filter datetime. Show entries aft

2019-03-01 11:54发布

问题:

I'm trying to filter a queryset to return all the entries where the date for each entry is after today. It's a calendar and I want this for an 'Upcoming Events' feature.

I do know that my code is completely wrong but I wrote it just to show you what I'm looking for.

now = datetime.datetime.now()
events = Event.objects.filter(date_from=now).order_by('date')

回答1:

You can use the __gte filter

Event.objects.filter(date_from__gte=now).order_by('date')

Django's ORM is smart enough to honor datetime v/s date