mar*_*and 6 migration django django-south
我试图从一个Django应用程序的几个模型迁移到另一个基于这个问题,我如何迁移模型出一个Django应用程序,进入一个新的?我已经有了很多工作但是在创建第一次迁移时我收到了这个错误:
"The model 'contenttype' from the app 'contenttypes' is not available in this migration."
Run Code Online (Sandbox Code Playgroud)
谷歌和SO似乎没有找到任何发生这种情况的案例,上述问题也没有任何具体的说法,除了代码中的评论:
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')
Run Code Online (Sandbox Code Playgroud)
真的很感激任何洞察我做错了什么.
以下是两个迁移文件:
创建:
# encoding: utf-8
import datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models
class Migration(SchemaMigration):
def forwards(self, orm):
db.rename_table('cars_country', 'general_country')
if not db.dry_run:
# For permissions to work properly after migrating
orm['contenttypes.ContentType'].objects.filter(app_label='cars', model='country').update(app_label='general')
def backwards(self, orm):
pass
Run Code Online (Sandbox Code Playgroud)
删除:
# encoding: utf-8
import datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models
class Migration(SchemaMigration):
depends_on = (
('general', '0002_create_country'),
)
def forwards(self, orm):
db.alter_column('cars_club', 'country_id', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['general.Country'], null=True))
def backwards(self, orm):
db.rename_table('general_country', 'cars_country')
db.alter_column('cars_club', 'country_id', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['cars.Country'], null=True))
Run Code Online (Sandbox Code Playgroud)
好的,找到解决方案了。来自 dgel 的冻结通知让我检查了 South 文档,那里\xe2\x80\x99s 有一个关于 ORM 迁移的通知:这是通过将模型序列化到每个迁移底部的一个名为 models 的大字典中来完成的。它\xe2\x80\x99很容易看到;它\xe2\x80\x99是底部的一大块密集代码。
\n\n所以基本上我只需要将 orm['contenttypes.contenttype] 移动到第二个迁移文件,因为 contenttype 模型字典已经在那里了。现在一切似乎都按其应有的方式进行。
\n 归档时间: |
|
查看次数: |
3422 次 |
最近记录: |