尝试在 Django 中使用 Content_type 自然键加载固定装置时出现类型错误
我在将模型之一转储到装置中时使用 --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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我现在只能猜测,但是在查看 Django 的源代码和错误消息之后,我认为您的装置的格式可能已损坏。您发布的示例是文件的全部内容吗?如果是,那么我认为您需要将该模型放入列表中,如下所示(注意外括号):
为什么? Django成功解析JSON数据后,该数据被传递给python反序列化器。如下迭代数据:
现在想象
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):
Why? After Django parsed the JSON data successfully, this data is passed to the python deserializer. This iterates over the data as follows:
Now imagine
object_list
is a json object (equivalent to python's dictionary), iterating over it will only get you the keys, in this casepk, model, field
. In line 84 Django does_get_model(d["model"])
, that is, using a string"model"
as index to another string, probablypk
(which is the first element inobject_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.