Skip to content

Instantly share code, notes, and snippets.

@kezabelle
Created September 24, 2013 11:16
Show Gist options
  • Save kezabelle/6683315 to your computer and use it in GitHub Desktop.
Save kezabelle/6683315 to your computer and use it in GitHub Desktop.
Expose a method on the paginator for treating the original input as chunks, without needing to have nested forloops. This is so that big querysets can be chunked into smaller result sets (ie: less memory usage), at the cost of increased query throughput. I thought this'd be harder.
from django.core.paginator import Paginator
class ChunkingPaginator(Paginator):
def chunked_objects(self):
"""
Usage:
bigqs = ChunkingPaginator(Model.objects.all(), 100)
for obj in bigqs.chunked_objects():
# do something with the obj
pass
"""
for page in self.page_range:
for obj in self.page(page):
yield obj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment