I am trying to update multiple files in fileField in django. I am fetching the objects from the database and i am assigning the new file by iteration over them and then, saving the object in the list. If i use bulk_update it updates all the fields including the FileFields but is not uploading the file and if i iterate over each object and use .save(), then its working fine.
But using .save() function hits the database multiple times. So, i need to use bulk_update
update_list = []
t_obj = FileFieldDetails.objects.filter(ques_key__in=q_key)
for t in t_obj.iterator():
t.value = request.FILES[0]
update_list.append(t)
# Not Working
FileFieldDetails.objects.bulk_update(update_list,['value'])
# Working
for i in update_list:
i.save()
You could wrap your for loop inside a transaction atomic.
Atomicity is the defining property of database transactions. atomic allows us to create a block of code within which the atomicity on the database is guaranteed. If the block of code is successfully completed, the changes are committed to the database. If there is an exception, the changes are rolled back.
Like so:
from django.db import transaction
with transaction.atomic():
for i in update_list:
i.save()
That could help you to mitigate the hits to your database