Paginate QuerySet in Django

To manage a process on big queryset without overload the DB and the RAM, set the following loop:

STEP=1000
all_objects = MyObject.objects.order_by("id").all()
total = my_objects.count()
offset = 0
limit = STEP
while offset < total:
    for my_object in my_objects[offset:limit]:
        # … do what you need to do …
    offset += STEP
    limit += STEP

⚠️ you need to set a order_by to the queryset to ensure all records are browsed

Leave a Reply

Your email address will not be published. Required fields are marked *