[Answered ]-Django update model and preventing full table lock

1👍

Well if you want to batch the update you can try sth like this:

batch_size = 10
total_rows = queryset.count()
i = 0

while processed_rows < total_rows:
    batch = queryset[i:i+ batch_size]
    batch.update(touched=now())

    i += batch_size

or if you want to use django bulk_update with batch option:

objs = []
for obj in queryset:
    obj.touched = now()
    objs.append(obj)
MyModel.objects.bulk_update(objs, ['touched'], batch_size=10)

NOTE: none of this are recommended and have pretty slow performance for large amount of data. use them with caution.

Leave a comment