【发布时间】:2012-11-28 00:54:13
【问题描述】:
我正在尝试将一些模型从一个 Django 应用程序迁移到另一个应用程序,并且基于这个问题 How do I migrate a model out of one django app and into a new one? 我已经完成了很多工作,但是在创建第一个迁移时我收到了这个错误:
"The model 'contenttype' from the app 'contenttypes' is not available in this migration."
Google 和 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')
非常感谢任何关于我做错了什么的见解。
这是两个迁移文件:
创建:
# 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
删除:
# 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))
【问题讨论】:
-
根据 South docs:请注意,您只能访问已冻结的模型; South 自动包含可以通过外键或多对多关系访问的任何内容,但如果您想添加其他模型,只需将 --freeze appname 传递给 ./manage.py 数据迁移命令。
-
所以听起来您需要在某个时候将 ContentTypes 应用程序传递给 --freeze。不完全理解如何,但可能是正确方向的提示。
-
假设 ContentTypes 应用程序不会成为任何架构迁移的目标,从 Django 项目导入模型有多糟糕? from django.contrib.contenttypes.models import ContentType ContentType.objects.filter(app_label='common', model='cat').update(app_label='specific')
标签: django migration django-south