Django documentation

15. Transactions

This example describes Django version 0.96. For the current example, go here.

Django handles transactions in three different ways. The default is to commit each transaction upon a write, but you can decorate a function to get commit-on-success behavior. Alternatively, you can manage the transaction manually.

Model source code

from django.db import models

class Reporter(models.Model):
    first_name = models.CharField(maxlength=30)
    last_name = models.CharField(maxlength=30)
    email = models.EmailField()

    def __str__(self):
        return "%s %s" % (self.first_name, self.last_name)

Sample API usage

This sample code assumes the above model has been saved in a file mysite/models.py.

>>> from mysite.models import Reporter

>>> from django.db import connection, transaction