假设我们有两个应用程序:通用和专用:
myproject/|-- common| |-- migrations| | |-- 0001_initial.py| | `-- 0002_create_cat.py| `-- models.py`-- specific |-- migrations | |-- 0001_initial.py | `-- 0002_create_dog.py `-- models.py
现在,我们要将模型common.models.cat移至特定的应用程序(精确地移至specific.models.cat)。首先在源代码中进行更改,然后运行:
$ python manage.py schemamigration specific create_cat --auto + Added model 'specific.cat'$ python manage.py schemamigration common drop_cat --auto - Deleted model 'common.cat'myproject/|-- common| |-- migrations| | |-- 0001_initial.py| | |-- 0002_create_cat.py| | `-- 0003_drop_cat.py| `-- models.py`-- specific |-- migrations | |-- 0001_initial.py | |-- 0002_create_dog.py | `-- 0003_create_cat.py `-- models.py
现在我们需要编辑两个迁移文件:
#0003_create_cat: replace existing forward and backward pre#to use just one sentence:def forwards(self, orm): db.rename_table('common_cat', 'specific_cat') if not db.dry_run: # For permissions to work properly after migrating orm['contenttypes.contenttype'].objects.filter( app_label='common', model='cat', ).update(app_label='specific')def backwards(self, orm): db.rename_table('specific_cat', 'common_cat') if not db.dry_run: # For permissions to work properly after migrating orm['contenttypes.contenttype'].objects.filter( app_label='specific', model='cat', ).update(app_label='common')#0003_drop_cat:replace existing forward and backward pre#to use just one sentence; add dependency:depends_on = ( ('specific', '0003_create_cat'),)def forwards(self, orm): passdef backwards(self, orm): pass现在,这两个应用程序的迁移都知道变化,而生活变得糟透了:-)在迁移之间设置这种关系是成功的关键。现在,如果您这样做:
python manage.py migrate common > specific: 0003_create_cat > common: 0003_drop_cat
将进行迁移,并且
python manage.py migrate specific 0002_create_dog < common: 0003_drop_cat < specific: 0003_create_cat
会向下迁移。
请注意,对于架构的升级,我使用了通用应用程序,而对于降级,则使用了特定应用程序。那是因为这里的依赖关系是如何工作的。



