Using Django, I want to find all published events after today, but only from the next month containing events

I have an event object that has from_date as a field that represents when the event starts. I want to do this to find an upcoming event, and then find the next upcoming events that are still within a month. Here are two questions that I still have, is there a way to combine them?

 today = datetime.date.today() date = Event.objects.filter( status='P', # Published status pub_date__lte=today, # Published after today, or today from_date__gte=today, # Starting next ).order_by('from_date').only('from_date')[:1][0].from_date events = Event.objects.filter( # Published after today, with a published status, and start today or later pub_date__lte=today, from_date__gte=today, status='P', # We're only going to show one month at a time. from_date__month=date.month, from_date__year=date.year, ) 
+4
source share
1 answer

I think that what you are already doing is actually quite effective. The Django query engine should collapse them into two SQL queries, one for each filter.

Getting stuck in just one SQL query doesn't always make it more efficient.

+1
source

Source: https://habr.com/ru/post/1343251/


All Articles