Django schema migration

Source: Internet
Author: User

Syncdb can only initialize table (create) and cannot actively update/delete/drop.

South came into being.


South simple usage:

Installation:

PIP install south


After installation, add it to installed_apps and syncdb it. OtherwiseSouth_migrationhistoryThe table cannot be found.

(Note: At this time, syncdb behavior is changed by south. Pay attention to the command line output. The structure of the table south_migrationhistory is as follows:


)


Trial installation:

Manage. py Shell

> Import south


Create a test_south app, create the model south_test, and add a field name. Add the app to installed_apps.

from django.db import modelsclass south_test(models.Model):    name = models.CharField(max_length=100)# Create your models here.


First use:

Manage. py schemamigrationYour_app-- Initial

Manage. py migrateYour_app

In this example, your_app is test_south, and the following is a magic horse (note that the yellow highlight part ):


In this step, only the migration file for creating database tables is generated, which is 000w.initial.py. The content is as follows:




This step generates a database model and adds a record to south_migrationhistory.

The data table south_migrationhistory inserts a record, for example:



Activate your own ORM:

Manage. py schemamigrationYour_app-- Auto

Manage. py migrateYour_app

 

For demonstration, if the demand changes today, we modify the test_south.south_test model and add the field memo, for example:

from django.db import modelsclass south_test(models.Model):    name = models.CharField(max_length=100)    memo = models.CharField(max_length=100)

Run schemamigration/migrate instead of syncdb, for example:


A 0002 file is created, for example:



Before migrate, let's look at the south_test table.


Run migrate, for example:


View the database table south_test again


Observe the south_migrationhistory table again, for example:


======================================

It is very easy to add South to an existing app. The command is as follows:

Manage. py convert_to_southYour_app

It only adds a record to the table south_migrationhistory (the Migration file is generated at the same time ).

If your_app here is books, the records of south_migrationhistory are as follows:


(Note: If the code is addedVersion Control--- For example, SVN, you need to run manage. py migrate after others download the code.MyApp 0001-- Fake: Since convert_to_south always tries to create all existing tables, this command tells South that MyApp has created these tables and does not need to create them again. In the future, follow the normal Migration:

Manage. py syncdb

If the model is changed, run

Manage. py shemamigrationMyApp-- Auto

Manage. py migrateMyApp

)



A lot of other information, can be admitted here: http://south.readthedocs.org/en/latest/tutorial/part1.html#tutorial-part-1


For more information, see http://blog.csdn.net/lion_awake/article/details/38283621,thank you!


Django schema migration

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.