尝试在 Django 中使用 Content_type 自然键加载固定装置时出现类型错误

发布于 2024-10-16 07:38:27 字数 1177 浏览 7 评论 0原文

我在将模型之一转储到装置中时使用 --natural 选项,这样在部署时就不会遇到 Content_typ ID 问题。结果在这里:

{
     "pk": 1, 
     "model": "seo.opportunitymetadatamodel", 
     "fields": {
         "_content_type": [
              "opportunity", 
              "jobopportunity"
         ], 
         "og_description": "", 
         "description": "", 
         "title": "test", 
         "keywords": "", 
         "og_title": "", 
         "heading": ""
     }
}

但是当我尝试加载夹具时,出现以下错误:

Problem installing fixture 'seo/fixtures/initial_data.json': Traceback (most recent call last):
  File "/Users/xx/dev/envs/xx/lib/python2.6/site-packages/django/core/management/commands/loaddata.py", line 167, in handle
    for obj in objects:
  File "/Users/xx/dev/envs/xx/lib/python2.6/site-packages/django/core/serializers/json.py", line 38, in Deserializer
    for obj in PythonDeserializer(simplejson.load(stream), **options):
  File "/Users/xx/dev/envs/xx/lib/python2.6/site-packages/django/core/serializers/python.py", line 84, in Deserializer
    Model = _get_model(d["model"])
TypeError: string indices must be integers, not str

似乎该方法不接受要加载的字符串。我缺少什么?

I am using the --natural option when dumping one of my model into a fixtures, so that I won't run into Content_typ ID problem when deploying. Results is here:

{
     "pk": 1, 
     "model": "seo.opportunitymetadatamodel", 
     "fields": {
         "_content_type": [
              "opportunity", 
              "jobopportunity"
         ], 
         "og_description": "", 
         "description": "", 
         "title": "test", 
         "keywords": "", 
         "og_title": "", 
         "heading": ""
     }
}

But when I try to load back the fixture, I get the following error:

Problem installing fixture 'seo/fixtures/initial_data.json': Traceback (most recent call last):
  File "/Users/xx/dev/envs/xx/lib/python2.6/site-packages/django/core/management/commands/loaddata.py", line 167, in handle
    for obj in objects:
  File "/Users/xx/dev/envs/xx/lib/python2.6/site-packages/django/core/serializers/json.py", line 38, in Deserializer
    for obj in PythonDeserializer(simplejson.load(stream), **options):
  File "/Users/xx/dev/envs/xx/lib/python2.6/site-packages/django/core/serializers/python.py", line 84, in Deserializer
    Model = _get_model(d["model"])
TypeError: string indices must be integers, not str

Seems like the method does not accept a string to load. What am I missing ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜扑 2024-10-23 07:38:27

我现在只能猜测,但是在查看 Django 的源代码和错误消息之后,我认为您的装置的格式可能已损坏。您发布的示例是文件的全部内容吗?如果是,那么我认为您需要将该模型放入列表中,如下所示(注意外括号):

[
  {
    "pk": 1, 
    "model": "seo.opportunitymetadatamodel", 
    "fields": {
      "_content_type": [
        "opportunity", 
        "jobopportunity"
      ], 
      "og_description": "", 
      "description": "", 
      "title": "test", 
      "keywords": "", 
      "og_title": "", 
      "heading": ""
    }
  }
]

为什么? Django成功解析JSON数据后,该数据被传递给python反序列化器。如下迭代数据:

82      for d in object_list:
83          # Look up the model and starting build a dict of data for it.
84          Model = _get_model(d["model"])

http://code.djangoproject。 com/browser/django/trunk/django/core/serializers/python.py#L82

现在想象 object_list 是一个 json 对象(相当于 python 的字典),迭代它只会得到键,在本例中为pk、模型、字段。在第 84 行,Django 执行 _get_model(d["model"]),即使用字符串 "model" 作为另一个字符串的索引,可能是 pk< /code> (这是 object_list 中的第一个元素)。这是一个类型错误。

object_list 是一个实际列表时,迭代它会得到字典,可以通过字符串索引。

I can only guess right now, but after looking at Django's source code and your error message I think the format of your fixture might be broken. The example you posted, is that the whole content of the file? If yes, then I think you need to put that model in a list, like this (pay attention to the outer brackets):

[
  {
    "pk": 1, 
    "model": "seo.opportunitymetadatamodel", 
    "fields": {
      "_content_type": [
        "opportunity", 
        "jobopportunity"
      ], 
      "og_description": "", 
      "description": "", 
      "title": "test", 
      "keywords": "", 
      "og_title": "", 
      "heading": ""
    }
  }
]

Why? After Django parsed the JSON data successfully, this data is passed to the python deserializer. This iterates over the data as follows:

82      for d in object_list:
83          # Look up the model and starting build a dict of data for it.
84          Model = _get_model(d["model"])

http://code.djangoproject.com/browser/django/trunk/django/core/serializers/python.py#L82

Now imagine object_list is a json object (equivalent to python's dictionary), iterating over it will only get you the keys, in this case pk, model, field. In line 84 Django does _get_model(d["model"]), that is, using a string "model" as index to another string, probably pk (which is the first element in object_list). That's a type error.

When object_list is an actual list, iterating over it will give you dictionaries, which can be indexed by strings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文