I'm running Django 1.4.11. I overrode the save()
method of a Django model in a way similar to the following code:
from django.db import models
from django.db import transaction
class MyModel(models.Model):
# model definition
@transaction.commit_manually
def save(self, *args, **kwargs):
try:
super(self.__class__, self).save(*args, **kwargs)
foo() # do_other_things
except:
transaction.rollback()
raise
else:
transaction.commit()
When I run my code, sometimes I read this message in the Apache log:
RemovedInDjango18Warning: commit_manually is deprecated in favor of set_autocommit.
How can I implement the same logic with set_autocommit?
The same logic would look like this:
However, this would be equivalent to using the
atomic()
decorator:This will commit the transaction on a successful
__exit__
, and roll back in the case of an exception.