简体   繁体   中英

Django Deserialization

I am getting the following error:

Traceback (most recent call last):
File "../tests.py", line 92, in test_single_search

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'

In views.py the serialization works correctly:

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:

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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