简体   繁体   English

Django 反序列化

[英]Django Deserialization

I am getting the following error:我收到以下错误:

Traceback (most recent call last):回溯(最近一次调用最后一次):
File "../tests.py", line 92, in test_single_search文件“../tests.py”,第 92 行,在 test_single_search 中

for return_obj in serializers.deserialize("json",response, ensure_ascii=False):对于 serializers.deserialize("json",response, ensure_ascii=False) 中的 return_obj:
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'文件“/Library/Python/2.6/site-packages/django/core/serializers/json.py”,第 38 行,在 PythonDeserializer(simplejson.load(stream), **options) 中的 obj 解串器中:文件“/System /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/ init .py”,第 264 行,加载返回加载(fp.read(), AttributeError: 'HttpResponse' object has no attribute '读'

In views.py the serialization works correctly:在 views.py 中,序列化工作正常:

resultsjson = serializers.serialize("json", results, ensure_ascii=False)
return HttpResponse(resultsjson, mimetype = 'application/json')

However, when I try to process the result in my calling method in test.py:但是,当我尝试在 test.py 中的调用方法中处理结果时:

response = self.client.get("/path/?query=testValue")
for return_obj in serializers.deserialize("json", response, ensure_ascii=False):
      print return_obj

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.我正在使用 Django 1.2(来自 svn 的最新版本),它似乎在使用内置的 simplejson 序列化器。

You need to use response.content rather than just response in your call to deserialize .您需要在调用deserialize使用response.content而不仅仅是response The response object is an instance of HttpResponse, but has an attribute of content which contains the actual JSON in this case.响应对象是 HttpResponse 的一个实例,但在本例中具有包含实际 JSON 的content属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM