Created
October 12, 2016 07:42
-
-
Save Petelin/dcbb2145d31cc1a0926e4f28a9e3000c to your computer and use it in GitHub Desktop.
django 拆分大 hit 为分批 hit 数据库
This file contains hidden or 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
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