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
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
2 matches
Mail list logo