views:

143

answers:

1

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.

+5  A: 

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.

Daniel Roseman
yes, try `serializers.deserialize("json", response.content, ensure_ascii=False)` as Daniel suggested
kRON
Thanks, using response.content solved the problem.
herbertyeung