Django ORM: move filter after annotate subquery – Postgresql
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 ( SELECT id, related_id, values, date_created ROW_NUMBER() OVER ( PARTITION BY related_id ORDER BY date_created DESC )…