Python 如何在Django中的ContentType外键上使用get_by_natural_key()加载数据设备?
为使用Python 如何在Django中的ContentType外键上使用get_by_natural_key()加载数据设备?,python,django,json,Python,Django,Json,为使用ContentType外键的模型加载外部数据装置时遇到问题 我在模型中使用了一个经理,就像医生说的那样。不幸的是,尽管文档中提到了在ContentType外键上使用get\u-by\u-natural\u-key方法的重要性,但它随后进入了一个不同的示例。我很难想象经理会是什么样子。我的最佳猜测是再次使用get\u by\u natural\u键,并指定app\u标签和model查找,但我可能会偏离方向 # models.py from django.db import models
ContentType
外键的模型加载外部数据装置时遇到问题
我在模型中使用了一个经理,就像医生说的那样。不幸的是,尽管文档中提到了在ContentType
外键上使用get\u-by\u-natural\u-key
方法的重要性,但它随后进入了一个不同的示例。我很难想象经理会是什么样子。我的最佳猜测是再次使用get\u by\u natural\u键
,并指定app\u标签
和model
查找,但我可能会偏离方向
# models.py
from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.utils.translation import ugettext_lazy as _
class InlineTypeManager(models.Manager):
def get_by_natural_key(self, title, app_label, model):
return self.get(title=title, content_type=ContentType.objects.get_by_natural_key(app_label=content_type__name, model=content_type__id))
class InlineType(models.Model):
title = models.CharField(_("title"), max_length=255)
content_type = models.ForeignKey(ContentType, limit_choices_to={"model__in": ("Link", "Document", "Image", "Audio", "Video", "MediaSet", "Flash", "Testimonial")})
objects = InlineTypeManager()
class Meta:
ordering = ["title"]
verbose_name = _("inline type")
verbose_name_plural = _("inline types")
def __unicode__(self):
return u"%s" % (self.title)
我的初始数据.json
:
[
{
"model": "inlines.inlinetype",
"pk": 1,
"fields": {
"title": "Image",
"content_type": "image"
}
}, {
"model": "inlines.inlinetype",
"pk": 2,
"fields": {
"title": "Video",
"content_type": "video"
}
}
]
当我loaddata
myjson时,我收到错误:
DeserializationError: [u"'image' value must be an integer."]
get_by_natural_key
的要点是在“人性化”查找中加载非整数字段,因为JSON中的硬编码ID是一个坏主意,因为它不可预测,所以我猜我的管理器失败了。或者我应该使用get\u for_model()
/get\u for_models()
?Django中的自然键是
外键和多对多关系的默认序列化策略是序列化关系中对象的主键值
对于那些不作为ForeignKey/ManyToManyField出现在要转储的目标中的模型,您不需要在Manager中实现诸如natural_key和get_by_natural_key之类的方法。因此可以删除InlineTypeManager()行 此外,转储的initial_data.json中的content_type字段的值不正确。Django只将列表视为自然键,类似“image”的字符串仍然视为代理键,并且将失败,因为它无法成功强制为int。正确的ContentType转储看起来像
from django.contrib.contenttypes.models import ContentType
from django.utils import simplejson
>>> simplejson.dumps(ContentType.objects.get(model='user').natural_key())
'["auth", "user"]'
Django中的自然键是 外键和多对多关系的默认序列化策略是序列化关系中对象的主键值
对于那些不作为ForeignKey/ManyToManyField出现在要转储的目标中的模型,您不需要在Manager中实现诸如natural_key和get_by_natural_key之类的方法。因此可以删除InlineTypeManager()行 此外,转储的initial_data.json中的content_type字段的值不正确。Django只将列表视为自然键,类似“image”的字符串仍然视为代理键,并且将失败,因为它无法成功强制为int。正确的ContentType转储看起来像
from django.contrib.contenttypes.models import ContentType
from django.utils import simplejson
>>> simplejson.dumps(ContentType.objects.get(model='user').natural_key())
'["auth", "user"]'
“对于那些在要转储的目标中不作为ForeignKey/ManyToManyField出现的模型,您不需要在Manager中实现诸如natural_key和get_by_natural_key之类的方法。”我很困惑……我的模型怎么不使用ForeignKey
InlineType
具有content\u type=models.ForeignKey(ContentType)
。你的答案似乎是关于倾销。我想loaddata
不转储它,因此我的JSON可能有点不正确,但答案是不要使用simplejson
@richec转储它。您转储的JSON不正确,例如“content\u type”:“video”,这会导致loaddata失败。json应该看起来像我在这里显示的转储结果,比如[“auth”;“user”]。我不是说不使用FK,我是说natural_key和get_by_natural_key方法只适用于通过FK指向的模型。在您的例子中,ContentType位于InlineType的FK字段中,因此ContentType需要实现自然_键并通过自然_键获取_,它已经拥有了自然_键。InlineType是您转储到JSON文件并从JSON文件中加载的目标模型,它没有被FK引用,因此不需要使用该方法!非常感谢你。在我的情况下,我的应用程序/模型对应该是[“媒体”、“图像”]和[“媒体”、“视频”]。我觉得奇怪的是,这些文档暗示,当ContentType自动提供时,需要一个管理器。再次感谢!“对于那些在要转储的目标中不作为ForeignKey/ManyToManyField出现的模型,您不需要在Manager中实现诸如natural_key和get_by_natural_key之类的方法。”我很困惑……我的模型怎么不使用ForeignKeyInlineType
具有content\u type=models.ForeignKey(ContentType)
。你的答案似乎是关于倾销。我想loaddata
不转储它,因此我的JSON可能有点不正确,但答案是不要使用simplejson
@richec转储它。您转储的JSON不正确,例如“content\u type”:“video”,这会导致loaddata失败。json应该看起来像我在这里显示的转储结果,比如[“auth”;“user”]。我不是说不使用FK,我是说natural_key和get_by_natural_key方法只适用于通过FK指向的模型。在您的例子中,ContentType位于InlineType的FK字段中,因此ContentType需要实现自然_键并通过自然_键获取_,它已经拥有了自然_键。InlineType是您转储到JSON文件并从JSON文件中加载的目标模型,它没有被FK引用,因此不需要使用该方法!非常感谢你。在我的情况下,我的应用程序/模型对应该是[“媒体”、“图像”]和[“媒体”、“视频”]。我觉得奇怪的是,这些文档暗示,当ContentType自动提供时,需要一个管理器。再次感谢!