简体   繁体   中英

Only process subset of a queryset in Django (and return original queryset)

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 Paginator class ( docs ), then you can request the current page and iterate over those objects in the view:

from django.core.paginator import Paginator

# in the view:
p = Paginator(objects, 2)
page = p.page(current_page)
for o in page.object_list:
    # do processing
    pass

You can obtain the value for current_page from the request's parameters (eg, a page parameter in request.GET ).

您应该对page.object_list的结果进行处理,该结果将保证仅包含该页面的对象。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM