Answer the question
In order to leave comments, you need to log in
How to filter the result of a window function in Django?
I annotate the selection with row numbers using a window function. I want the line number to remain the same after filtering.
In this case, filtering occurs before the window function is applied:
from django.db.models import F, Window
from django.db.models.functions import RowNumber
queryset.annotate(
num=Window(RowNumber(), order_by=F('field').asc()),
).filter(name__startswith='A')
ids_nums = queryset.annotate(
num=Window(RowNumber(), order_by=F('field').asc()),
).values('id', 'num')
queryset.filter(name__startswith='A').annotate(num=???)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question