使用南冻结时,南数据迁移'实例'错误

Dar*_*ryl 4 django data-migration django-south

我有一个南数据迁移,试图根据其他模型中的数据创建新对象.在尝试为给定的"目标"模型创建新对象时,我不断获得:

Cannot assign "<ContentType: ContentType object>": "Publishing.content_type" must be a "ContentType" instance.
Run Code Online (Sandbox Code Playgroud)

当通过South冻结ORM访问时,"实例"似乎有问题,例如:

ContentType = orm['contenttypes.ContentType']
content_type_kwargs = {
    'model': ContentModel._meta.module_name, 
    'app_label': ContentModel._meta.app_label, }
content_type = ContentType.objects.get(**content_type_kwargs)

# further down

publishing_kwargs = {
    'site': Site.objects.get_current(),
    'publishing_type': publishing_type,
    'start': start,
    'content_type': content_type, 
    'object_id': content_object.id, }

publishing = orm.Publishing(**publishing_kwargs) # Produces the error above
Run Code Online (Sandbox Code Playgroud)

现在我已多次验证它content_type实际上是ContentType的一个实例 - 但不知何故django并不这么认为.

  • "冻结",南方orm版本的实例与原生django版本之间有区别吗?
  • 这可能是什么呢?

Chr*_*att 8

这是由于南方处理模型的方式.您必须冻结迁移中需要使用的任何模型.应用程序中迁移所在的模型会自动冻结; 你必须手动冻结的任何东西:

python manage.py schemamigration --auto yourapp --freeze contenttypes
Run Code Online (Sandbox Code Playgroud)

如果您需要冻结多个应用,请根据需要多次重复--freeze参数:

python manage.py schemamigration --auto yourapp --freeze contenttypes --freeze someotherapp ...
Run Code Online (Sandbox Code Playgroud)

另一件事.当您访问这些额外的冻结模型时,您必须使用旧式South API:

orm['contenttypes.contenttype'].objects.all()
Run Code Online (Sandbox Code Playgroud)

有点像orm.ContentType不行.