How to filter a Window query?

45 views
Skip to first unread message

Tom J. Wassing

unread,
Jan 1, 2020, 1:37:24 PM1/1/20
to Django users
I am search for a solution to filter the results of a query which uses
a window function. I would like to reduce the results set to only
include the entries where the row number is smaller than 10. It is not
supported by the ORM to filter directly on this query set
(https://code.djangoproject.com/ticket/28333). Is there a workaround
for this?

For example (not working):

row_number = Window(
    expression=RowNumber(),
    output_field='row',
    partition_by=(TruncDate('created')),
    order_by=F('created').desc())

posts_with_row = (Post.objects
         .annotate(row=row_number)
         .filter(row_number__range=(1, 10)))

Best,

Tom
Reply all
Reply to author
Forward
Message has been deleted
0 new messages