[英]Django deserialization error: string indices must be integers
I'm new to json, Python and Django. 我是json,Python和Django的新手。 I did some research online but none solves my issue. 我在网上做了一些研究,但没有一个能解决我的问题。 Thanks in advance for any insights! 预先感谢您的任何见解!
I am building a system that allows mobile devices to update the server's database, which is managed by Django. 我正在建立一个允许移动设备更新服务器数据库的系统,该系统由Django管理。 I am currently only testing on my local machine where I send a request to a url Django recognizes. 我目前仅在本地计算机上测试,在该计算机上我向Django识别的URL发送请求。
The 1st step I have a piece of python code that tries to communicate with the server. 第一步,我有一段python代码试图与服务器通信。
# in test.py:
data = '''{"pk": 4, "model": "arts"}'''
data = json.loads(data)
data = json.dumps(data)
URL = "my local host's URL"
h = httplib2.Http(".cache")
resp, content = h.request(URL, "POST", body = data)
Then at the server the view function is called. 然后在服务器上调用view函数。
# in views.py:
def Updates(request, category):
if request.method=='POST':
print 'Data: %s' % request.body
## this prints successfully:
## > Data: {"pk": "4", "model": "arts"}
resultJson = serializers.deserialize('json', request.body)
for obj in resultJson:
print "OK"
return HttpResponse(request.body)
else:
return HttpResponse("Wrong Method")
The error message I got is: 我得到的错误信息是:
Django Version: 1.6.2
Exception Type: DeserializationError
Exception Value: string indices must be integers
...
Traceback Switch to copy-and-paste view
C:\Python27\lib\site-packages\django\core\handlers\base.py in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
C:\Python27\lib\site-packages\django\views\decorators\csrf.py in wrapped_view
return view_func(*args, **kwargs)
C:\pathToViewsFile\views.py in Updates
for obj in resultJson:
C:\Python27\lib\site-packages\django\core\serializers\json.py in Deserializer
six.reraise(DeserializationError, DeserializationError(e), sys.exc_info()[2])
C:\Python27\lib\site-packages\django\core\serializers\json.py in Deserializer
for obj in PythonDeserializer(objects, **options):
C:\Python27\lib\site-packages\django\core\serializers\python.py in Deserializer
Model = _get_model(d["model"])
As shown in the example JSON file Django's doc provides (that's the last place I checked, actually), deserialize
takes a list of dictionnaries (which should have a fields
key, by the way): 如Django的文档提供的示例JSON文件示例所示(实际上,这是我检查的最后一个位置), deserialize
将使用字典列表(顺便说一句,该字典应具有fields
键):
[
{
"pk": "4b678b301dfd8a4e0dad910de3ae245b",
"model": "sessions.session",
"fields": {
"expire_date": "2013-01-16T08:16:59.844Z",
...
}
}
]
You can also see in deserialize
's doc that this function returns an iterator. 您还可以在deserialize
的doc中看到此函数返回一个迭代器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.