The Django database complements the business

Source: Internet
Author: User

When you write data to a database, you inadvertently write incomplete data, which we call dirty data. Transaction management (transaction) can prevent this from happening. Transaction management Once a write exception is detected, a rollback operation is performed, either writing the full data or not writing it. The use of transactions in Django is simple:

1. New project transaction, create app App01, edit models Create two tables and perform a database migration as follows:

 from Import Models class UserInfo (models. Model):    = models. Charfield (max_length=32)    = models. Emailfield (max_length=64)class  Dept (models. Model):    = models. Charfield (MAX_LENGTH=32)

2. Define the route:

 from Import URL  from Import  = [    url (r'^test/$', Views.test),]

3. Define the test view function;

 fromDjango.shortcutsImportRender, HttpResponse from.ImportModelsdefTest (Request):Try:         fromDjango.dbImportTransaction#Import TransactionsWith transaction.atomic (): User_obj= Models. UserInfo.objects.create (username='Lena', email='[email protected]') Dept_obj= Models. Dept.objects.create (title='IT')    exceptException as E:returnHttpResponse ('error happened, DB rollback')    returnHttpResponse ('OK')

Description

    1. The above combines the created user_obj and dept_obj recorded behavior into an indivisible atomic operation that performs a rollback operation if any exception occurs in the database operation performed within the atom.
    2. Things are checked for exception rollback, but not fault-tolerant, errors are thrown, so here is an exception catch.
    3. Normal access http://127.0.0.1:8000/test/ , will get an OK response, if we manually dept_obj = models.Dept.objects.create(title=‘IT‘) change to title=‘IT‘ name=‘IT‘ , manually caused the exception, then the thing will trigger rollback, write user_obj will also be revoked. This allows you to view database validation.

  Note : Transactions require support from the database engine, such as the InnoDB engine.

The Django database complements the business

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.