Django, queryset filter datetime. Show entries aft

2019-03-01 11:25发布

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条回答
劫难
2楼-- · 2019-03-01 12:04

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

查看更多
登录 后发表回答