Created
December 14, 2012 03:36
-
-
Save netfl0/4282542 to your computer and use it in GitHub Desktop.
Django should really have this built in ...
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
class IterableQuerySet(object): | |
"""Allows iteration over a QuerySet breaking it off into smaller chunks.""" | |
"""Take from http://justcramer.com/2009/02/09/large-sql-result-sets-in-django/""" | |
def __init__(self, queryset, batch=10000): | |
self.batch = batch | |
self.queryset = queryset | |
def __iter__(self): | |
at = 0 | |
results = self.queryset[at:at+self.batch] | |
while results: | |
for result in results: | |
yield result | |
at += self.batch | |
results = self.queryset[at:at+self.batch] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment