Djangosaur
Set MySQL storage engine to InnoDB for Django transactions

The advantage of InnoDB tables is transactions.

If you make multiple edits to the database and something goes wrong, you won’t be left with half the data updated. Either everything gets written or nothing.

Open your settings file:

DATABASES = { 
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': '',                      
        'USER': '',     
        'PASSWORD': '',
        'OPTIONS': {
               "init_command": "SET storage_engine=INNODB",
        }   
    }   
}

Convert MyISAM tables to InnoDB

If your have already created your tables and they are using MyISAM, convert them.

python manage.py dbshell
SHOW TABLES;
SHOW CREATE TABLE <tablename>;
ALTER TABLE <tablename> ENGINE=INNODB;

Using transactions with django

# my_app/views.py
from django.db import transaction

@transaction.commit_on_success
def my_view(request):
    ....

Links

http://docs.djangoproject.com/en/dev/ref/databases/#creating-your-tables

http://docs.djangoproject.com/en/dev/topics/db/transactions/

http://en.wikipedia.org/wiki/Database_transaction

  1. djangosaur posted this