The default behavior of Djangos should be performed with an open transaction, which is automatically committed when you call any built-in data modification function. In the case of commit_on_success or commit_manual, django does not execute commit (), but rather successfully executes a function or the transaction.commit () command, respectively.
Thus, an elegant approach would be to separate the transaction processing code and another time code if possible:
from django.db import transaction from my_app.models import MyModel @transaction.commit_on_success def do_transaction(instances_to_save): for inst in instances_to_save: inst.save() def model_altering_method(): instances_to_save = [] for inst in MyModel.objects.all()[0:5000]: inst.name = 'Joel Spolsky'
If this is impossible constructively, for example. you need the information instance.id, which for new instances you can get only after the first save (), try to split the stream into reasonable sizes of work items, so as not to keep the transaction open for long minutes.
Also note that long transactions are not always bad. If your application is the only entity modifying db, this may actually be normal. However, you should check the specific configuration of your db to see the time limits for transactions (or inaction transactions).
Jonathan
source share