Re: Django ORM: move filter after annotate subquery

2023-04-28 Thread Aivan Fouren
I found a way to achieve the results I want: applying a filter, ` date_created__lte` in this example, outside of annotated query: sub = Model.objects.all() \ .annotate(ord=Window( expression=RowNumber(), partition_by=F('related_id'), order_by=[F('date_created').desc()] ) ) \ .fil

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