Skip to content

Instantly share code, notes, and snippets.

@netfl0
Created December 14, 2012 03:36
Show Gist options
  • Save netfl0/4282542 to your computer and use it in GitHub Desktop.
Save netfl0/4282542 to your computer and use it in GitHub Desktop.
Django should really have this built in ...
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