Skip to content

Instantly share code, notes, and snippets.

@zhoudaxia233
Created August 6, 2022 16:08
Show Gist options
  • Save zhoudaxia233/058190ffe6641d36ffda570726bb65bb to your computer and use it in GitHub Desktop.
Save zhoudaxia233/058190ffe6641d36ffda570726bb65bb to your computer and use it in GitHub Desktop.
django batch queryset
def batch_queryset(queryset, batchsize=2000):
queryset = queryset.order_by("pk")
total = queryset.count()
with transaction.atomic():
for start in range(0, total, batchsize):
end = min(start + batchsize, total)
yield queryset[start: end]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment