Created
September 24, 2013 11:16
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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