如何将模型从一个django应用程序迁移到一个新的应用程序? [英] How do I migrate a model out of one django app and into a new one?
问题描述
另外,请记住,我将需要这个是可重复的过程,使我可以迁移生产系统等。
如何使用南迁移
两个应用程序:普通和具体:
myproject /
| - common
| | - 迁移
| | | - 0001_initial.py
| | ` - 0002_create_cat.py
| ` - models.py
` - 具体的
| - 迁移
| | - 0001_initial.py
| ` - 0002_create_dog.py
` - models.py
现在我们要移动将common.models.cat模型转换为特定应用程序(正确到specific.models.cat)。
首先在源代码中进行更改,然后运行:
$ python manage .py schemamigration specific create_cat --auto
+添加模型'specific.cat'
$ python manage.py schemamigration common drop_cat --auto
- 删除的模型'common.cat'
myproject /
| - common
| | - 迁移
| | | - 0001_initial.py
| | | - 0002_create_cat.py
| | ` - 0003_drop_cat.py
| ` - models.py
` - 具体的
| - 迁移
| | - 0001_initial.py
| | - 0002_create_dog.py
| ` - 0003_create_cat.py
` - models.py
现在我们需要编辑两个迁移文件:
#0003_create_cat:替换现有的向前和向后代码
#to使用一个句子:
def forward(self,orm):
db.rename_table('common_cat','specific_cat')
如果不是db.dry_run :
#为了在迁移
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')
如果不是db.dry_run:
#为了在迁移
orm ['contenttypes.contenttype']之后正常工作的权限。objects.filter(
app_label ='specific',
model ='cat ,
).update(app_label ='common')
#0003_drop_cat:替换现有的前向和后向代码
#只使用一个句子;添加依赖关系:
depends_on =(
('specific','0003_create_cat'),
)
def forward(self,orm):
pass
def backwards(self,orm):
pass
现在这两个应用程序迁移意识到变化和生活只有一点点:-)
设置迁移之间的这种关系是成功的关键。
现在,如果你这样做:
python manage.py migrate common
>具体:0003_create_cat
> common:0003_drop_cat
将同时进行迁移,
python manage.py migrate specific 0002_create_dog
< common:0003_drop_cat
<具体:0003_create_cat
将迁移事物。
请注意,对于升级模式,我使用常用的应用程序和降级,我使用了特定的应用程序。这是因为这里的依赖关系如何。
I have a django app with four models in it. I realize now that one of these models should be in a separate app. I do have south installed for migrations, but I don't think this is something it can handle automatically. How can I migrate one of the models out of the old app into a new one?
Also, keep in mind that I'm going to need this to be a repeatable process, so that I can migrate the production system and such.
How to migrate using south.
Lets say we got two apps: common and specific:
myproject/
|-- common
| |-- migrations
| | |-- 0001_initial.py
| | `-- 0002_create_cat.py
| `-- models.py
`-- specific
|-- migrations
| |-- 0001_initial.py
| `-- 0002_create_dog.py
`-- models.py
Now we want to move model common.models.cat to specific app (precisely to specific.models.cat). First make the changes in the source code and then run:
$ 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
Now we need to edit both migration files:
#0003_create_cat: replace existing forward and backward code
#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 code
#to use just one sentence; add dependency:
depends_on = (
('specific', '0003_create_cat'),
)
def forwards(self, orm):
pass
def backwards(self, orm):
pass
Now both apps migrations are aware of the change and life sucks just a little less :-) Setting this relationship between migrations is key of success. Now if you do:
python manage.py migrate common
> specific: 0003_create_cat
> common: 0003_drop_cat
will do both migration, and
python manage.py migrate specific 0002_create_dog
< common: 0003_drop_cat
< specific: 0003_create_cat
will migrate things down.
Notice that for upgrading of schema I used common app and for downgrading, I used specific app. That's because how the dependency here works.
这篇关于如何将模型从一个django应用程序迁移到一个新的应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!