I have a generic list view in Django which returns around 300 objects (unless a search is performed).
I have pagination set to only display 10 of the objects.
So I query the database, then iterate through the objects, processing them and adding extra values before the display. I noticed that all 300 objects get the processing done, and the pagination is done after the processing. So I want to only do the processing on the objects that are going to displayed to increase performance.
I calculate the indexes of the objects in the queryset that should be processed 0-10 for page 1, 11-20 for page 2, 21-30 for page 3, etc.
Now I want to process only the objects in the display range but return the full queryset (so the genreic view works as expected).
Initially I tried:
for object in queryset[slice_start:slice_end] :
# process things here
return queryset
But the slicing seems to return a new queryset, and the original queryset objects do not have any of the calculated values.
Currently my solution is:
index = -1
for object in queryset:
index += 1
if index < slice_start or index > slice_end : continue
# process things here
return queryset
Now this works, but it seems rather hacky, and unelegant for Python.
Is there a more pythonic way to do this?
If you’re using Django’s
Paginatorclass (docs), then you can request the current page and iterate over those objects in the view:You can obtain the value for
current_pagefrom the request’s parameters (e.g, apageparameter inrequest.GET).