gpt4 book ai didi

django - contenttypes.contenttype 在此迁移中不可用

转载 作者:行者123 更新时间:2023-12-04 15:24:38 31 4
gpt4 key购买 nike

我正在尝试将一些模型从一个 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."

谷歌和 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))

最佳答案

好的,找到了解决方案。来自 dgel 的卡住通知让我检查了 South 文档,并且有一条关于 ORM 迁移的通知:这是通过将模型序列化到每次迁移底部称为模型的大字典来实现的。很容易看到;它是底部的大块密集代码。

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

关于django - contenttypes.contenttype 在此迁移中不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13595991/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com