Django ORM: move filter after annotate subquery

2023-04-28 Thread Aivan Fouren
This Django ORM statement: Model.objects.all() \ .annotate( ord=Window( expression=RowNumber(), partition_by=F('related_id'), order_by=[F("date_created").desc()] ) \ .filter(ord=1) \ .filter(date_created__lte=some_datetime) Leads to the following SQL query: SELECT * FROM

Re: Django ORM: move filter after annotate subquery

2023-04-28 Thread Aivan Fouren
y, parse values by Django ORM, but this looks too heavy for a simple nested subquery. So, a better version is appreciated 🙏 On Friday, April 28, 2023 at 4:52:16 PM UTC+3 Aivan Fouren wrote: > This Django ORM statement: > Model.objects.all() \ > .annotate( ord=Window( >