【问题标题】:contenttypes.contenttype not available in this migrationcontenttypes.contenttype 在此迁移中不可用
【发布时间】: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


【解决方案1】:

好的,找到了解决方案。来自 dgel 的冻结通知让我检查了 South 文档,并且有一个关于 ORM 迁移的通知:这是通过在每次迁移的底部将模型序列化到一个名为 models 的大字典中来完成的。很容易看到;这是底部的一大块密集代码。

所以基本上我只需要将 orm['contenttypes.contenttype] 移动到第二个迁移文件,因为 contenttype 模型字典已经存在。现在一切似乎都正常了。

【讨论】:

  • 或者简单地将您的数据迁移创建为:./manage.py datamigration app_name --auto --freeze contenttypes
猜你喜欢
  • 1970-01-01
  • 2013-12-09
  • 1970-01-01
  • 1970-01-01
  • 2012-12-10
  • 2021-03-31
  • 2015-03-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多