【问题标题】:South data migration 'instance' error when using south freeze orm使用南冻结 orm 时出现南数据迁移“实例”错误
【发布时间】:2011-06-02 06:03:05
【问题描述】:

我有一个南方数据迁移,它试图根据在其他模型中找到的数据创建新对象。当尝试为给定的“目标”模型创建一个新对象时,我不断得到:

Cannot assign "<ContentType: ContentType object>": "Publishing.content_type" must be a "ContentType" instance.

通过South freeze 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

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

  • 实例的“冻结”South orm 版本与原生 django 版本之间有区别吗?
  • 这还可能是什么?

【问题讨论】:

  • 达里尔,我面临同样的问题 goo.gl/I7Jj6 你设法解决了吗?我现在正在尝试使用 content_type 和 object_id 在数据迁移中创建一个新实例,但得到您在上面发布的确切错误。

标签: django data-migration django-south


【解决方案1】:

这是由于 South 处理模型的方式。您必须冻结在迁移中需要使用的任何模型。迁移所在的应用程序中的模型会自动冻结;您必须手动冻结的其他任何内容:

python manage.py schemamigration --auto yourapp --freeze contenttypes

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

python manage.py schemamigration --auto yourapp --freeze contenttypes --freeze someotherapp ...

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

orm['contenttypes.contenttype'].objects.all()

orm.ContentType 之类的东西不起作用。

【讨论】:

  • 感谢@chrisdpratt 的回答,尽管这更像是检查所有内容是否已“插入”,可以这么说。我面临的“技巧”是 South 的 orm 提供的“实例”,例如orm['contenttypes.contenttype'].objects.get(pk=1) != ContentType.objects.get(pk=1) -- 至少在用作创建另一个模型实例的参数时,例如上面的发布。它很奇怪,但它让我卡住了。
  • 这确实很奇怪。您使用的所有东西都必须来自 South ORM,但是一旦满足该条件,一切都应该正常工作。我只在尝试导入某些外部模型并使用它而不是冻结它并通过 ORM 获取它时才看到该特定错误。也许你应该联系南方队;你可能偶然发现了一些错误。
  • 谢谢@chrispratt,我向南south.aeracode.org/ticket/781提出了一个问题
猜你喜欢
  • 2014-05-29
  • 2014-07-30
  • 2011-11-19
  • 2010-12-25
  • 2012-11-21
  • 2011-04-03
  • 2013-11-17
  • 2011-02-20
相关资源
最近更新 更多