Django反序列化 [英] Django Deserialization
问题描述
我收到以下错误:
追溯(最近呼叫最后):
文件。 ./tests.py,第92行,test_single_search
Traceback (most recent call last):
File "../tests.py", line 92, in test_single_search
在serializers.deserialize(json,response,ensure_ascii = False)中的return_obj:
File/Library/Python/2.6/site-packages/django/core/serializers/json.py,
第38行,在Deserializer
中为PythonDeserializer中的obj(simplejson.load(stream) ,
** options):文件/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/init.py,
行264,加载
返回加载(fp.read(),AttributeError:'HttpResponse'对象
没有属性'read'
for return_obj in serializers.deserialize("json",response, ensure_ascii=False):
File "/Library/Python/2.6/site-packages/django/core/serializers/json.py",
line 38, in Deserializer
for obj in PythonDeserializer(simplejson.load(stream),
**options): File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/init.py",
line 264, in load
return loads(fp.read(), AttributeError: 'HttpResponse' object
has no attribute 'read'
在views.py中,序列化工作正常:
In views.py the serialization works correctly:
resultsjson = serializers.serialize("json", results, ensure_ascii=False)
return HttpResponse(resultsjson, mimetype = 'application/json')
但是,当我尝试在我的调用方法中处理test.py中的结果:
However, when I try to process the result in my calling method in test.py:
response = self.client.get("/path/?query=testValue")
for return_obj in serializers.deserialize("json", response, ensure_ascii=False):
print return_obj
我收到上述错误。有人遇到同样的错误。我正在使用Django 1.2(svn的最新版本),它似乎是使用内建的simplejson串行编码器。
I get the above error. Has anyone come across the same error. I am using Django 1.2 (latest version from svn) and it appears to be using the in-built simplejson serializser.
推荐答案
你您需要使用 response.content
而不仅仅是响应
在 deserialize
。响应对象是HttpResponse的一个实例,但它的属性为 content
,其中包含实际的JSON。
You need to use response.content
rather than just response
in your call to deserialize
. The response object is an instance of HttpResponse, but has an attribute of content
which contains the actual JSON in this case.
这篇关于Django反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!