Wednesday, January 1, 2020

How to filter a Window query?

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

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/1674aa7c-6e0e-4157-9178-16bc34226bb0%40googlegroups.com.

No comments:

Post a Comment