Skip to content

Instantly share code, notes, and snippets.

@ssteuteville
ssteuteville / batch_qs.gist
Created April 1, 2016 22:55
batching a query set in django
def batch_query_set(query_set, single=False, batch_size=1000):
try:
last_id = query_set.order_by('-id')[0].id
first_id = query_set.order_by('id')[0].id
for start in range(first_id, last_id + 1, batch_size):
end = min(start + batch_size - 1, last_id)
records = query_set.filter(id__range=(start, end)).all()
if single:
for record in records:
yield record

Django Idioms

General Tips

  • put methods that apply to a single instance of a Model on the model class itself
  • put methods meant to query against a Models entire table that models corresponding managers.py file.
  • if a models.py file gets too long to manage then create a models directory and place an __init__ file in it.
  • try to avoid using signals, but if you must then put them in a signals.py file