The use of South in django1.6
Data merge is already supported in django1.7, so it is only possible to use South in the django1.4 1.5 1.6 version number. South's main role is to do data merging, and when we define a model in Django, we use SYNCDB to synchronize to the database. The field of the model is then assumed to have changed. or a field property, you can't use syncdb enough. If you want to migrate the data, you need to build the library again, migrate the original data to the new library, South can help you take the initiative to complete these operations. has not been used before, now with the Django version number is still 1.6, the recent development of the model is not very stable time, so take it.
Description
- django1.6
- Win7
- Database MySQL 5.6
Installation
Install with PIP
pip install south
Let's say upgrade Sourh (1.0 should be the last version)
pip install south --upgrade
Django in configuration 1
Add a new app (Installed_apps config) to your Django project's settings file
INSTALLED_APPS = ( ... ‘south‘,)
2
Then use the Python manage.py shell under the project to open the Django shell
In [1]: import south
Assuming there are no errors, the installation configuration is good.
3
Use the required table to synchronize South with in the database before using it.
python manage.py syncdb
This table is already south_migrationhistory in the database.
Use
This is an already developed project, and very many tables and table structures have been established and are synchronized with SYNCDB to the database.
The name of the app is ADSR. There is a model for
class AdDailyReport(models.Model): ad = models.ForeignKey(Ad, on_delete=models.PROTECT) ddate = models.DateField(auto_now=False, auto_now_add=False, verbose_name=u‘统计日期‘) pv = models.IntegerField(default=0, verbose_name=u‘展现量‘) pc = models.IntegerField(default=0, verbose_name=u‘点击量‘) cost = models.DecimalField(null=True,max_digits=10, decimal_places=2, verbose_name=u‘花费‘) addtime = models.DateTimeField(auto_now_add=True)
Initialize Merge
South has its own initiative also has the manual merger way. Here we use our own way of being active
Create a new app and use South without syncdb
Existing apps, the database already has a table case
In the absence of any model changes, the existing initialization:
E:\hawk>python manage.py convert_to_south adsrThis application is already managed by South.
Then you have the ability to change, merge, merge, and apply the same models as the new app.
Change model
The model finally joins a field that records the update time
updatetime = models.DateTimeField(auto_now=True)
South Change Mode, application
E:\hawk>python manage.py schemamigration adsr --auto
gave some hints.
E:\hawk>python manage.py schemamigration adsr --auto ? The field ‘AdDailyReport.updatetime‘ does not have a default specified, yet is NOT NULL. ? Since you are adding this field, you MUST specify a default ?
value to use for existing rows. Would you like to: ?
1. Quit now, and add a default to the field in models.py ?
2. Specify a one-off value to use for existing columns now ? Please select a choice:
Google next StackOverflow on the answer, and then for example the following actions
?
Please select a choice: 2 ? Please enter Python code for your one-off default value. ? The datetime module is available, so you can do e.g. datetime.date.today() >>> datetime.datetime.now() + Added field updatetime on adsr.AdDailyReportCreated 0002_auto__add_field_addailyreport_updatetime.py. You can now apply this migration with: ./manage.py migrate adsr
This creates the new data model. Then the application.
E:\hawk>python manage.py migrate adsr
Apply the table change and data merge. This generates the new table structure and migrates the data on its own initiative.
Ref
High-level applications
This article is derived from"Orangleliu Notebook" Blog, reproduced please be sure to keep this source http://blog.csdn.net/orangleliu/article/details/40394925
Copyright notice: This article Orangleliu (http://blog.csdn.net/orangleliu/) original article. Articles reproduced in the declaration.
[Django1.6]south used in django1.6