Skip to content

Instantly share code, notes, and snippets.

@Petelin
Created October 12, 2016 07:42
Show Gist options
  • Save Petelin/dcbb2145d31cc1a0926e4f28a9e3000c to your computer and use it in GitHub Desktop.
Save Petelin/dcbb2145d31cc1a0926e4f28a9e3000c to your computer and use it in GitHub Desktop.
django 拆分大 hit 为分批 hit 数据库
from django.core.paginator import Paginator
def chunked_iterator(queryset, chunk_size=10000):
paginator = Paginator(queryset, chunk_size)
for page in range(1, paginator.num_pages + 1):
for obj in paginator.page(page).object_list:
yield obj
for event in chunked_iterator(Event.objects.all()):
print event
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment