Skip to content

Instantly share code, notes, and snippets.

@robdmc
Last active August 29, 2015 14:18
Show Gist options
  • Save robdmc/e14679d3102605928d6d to your computer and use it in GitHub Desktop.
Save robdmc/e14679d3102605928d6d to your computer and use it in GitHub Desktop.
Queryset chunker for values queries
def chunk_values_queryset(queryset, chunksize):
if queryset.count() > 0:
ending_pk = queryset.aggregate(Min('id'))['id__min'] - 1
out = ['dummy']
while len(out) > 0:
out = list(queryset.filter(pk__gt=ending_pk).order_by('id')[:chunksize])
if out:
ending_pk = out[-1]['id']
yield out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment