I think what you're looking for is limiting your set of queries .
Quote from the link above:
Use a subset of the Pythons array syntax to limit the number of QuerySet queries to a specific number of results. This is the equivalent of the LIMIT SQL and OFFSET clauses.
In other words, if you start with an account, you can then iterate and take fragments as needed.
cnt = MyObject.objects.count() start_point = 0 inc = 5 while start_point + inc < cnt: filtered = MyObject.objects.all()[start_point:inc] start_point += inc
Of course, you may need to handle the error again.
Sayse source share