Created
August 6, 2022 16:08
-
-
Save zhoudaxia233/058190ffe6641d36ffda570726bb65bb to your computer and use it in GitHub Desktop.
django batch queryset
This file contains 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
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