jeudi 30 avril 2015

Django REST framework: AttributeError: object has no attribute 'serializer'

I have some issues with writing tests against Django REST API framework. I have Python 2.7.6 on Mac OS v.10.10.3, Django==1.6.5 and djangorestframework==3.1.1. I'm trying to write a couple of tests against Django REST framework.

I have a dummy test view for the API:

class TestApiView(APIView):
    def get(self, request, format=None):
        return Response(status=status.HTTP_404_NOT_FOUND) 

It works just fine while I'm calling it:

(venv)bash-3.2# curl -H "Accept: application/json; indent=2" -i --request GET "http://ift.tt/1JcqKCi"
HTTP/1.0 404 NOT FOUND
Date: Thu, 30 Apr 2015 18:35:09 GMT
Server: WSGIServer/0.1 Python/2.7.6
Vary: Accept, Cookie
Allow: GET, HEAD, OPTIONS

But when I want to lunch a Test against that view it raises exception. My test code:

from rest_framework.test import APIClient

client = APIClient()
print client.get('/api/v1/client/test/', format='json')

This code raises exception:

Internal Server Error: /api/v1/client/test/
Traceback (most recent call last):
  File "/Users/user/dev/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 137, in get_response
    response = response.render()
  File "/Users/user/dev/venv/lib/python2.7/site-packages/django/template/response.py", line 105, in render
    self.content = self.rendered_content
  File "/Users/user/dev/venv/lib/python2.7/site-packages/rest_framework/response.py", line 59, in rendered_content
    ret = renderer.render(self.data, media_type, context)
  File "/Users/user/dev/venv/lib/python2.7/site-packages/rest_framework/renderers.py", line 333, in render
    form = data.serializer
AttributeError: 'NoneType' object has no attribute 'serializer'
Traceback (most recent call last):
  File "/Users/user/dev/managerv2/tests.py", line 20, in <module>
    print client.get('/api/v1/client/test/', format='json')
  File "/Users/user/dev/venv/lib/python2.7/site-packages/rest_framework/test.py", line 160, in get
    response = super(APIClient, self).get(path, data=data, **extra)
  File "/Users/user/dev/venv/lib/python2.7/site-packages/rest_framework/test.py", line 86, in get
    return self.generic('GET', path, **r)
  File "/Users/user/dev/venv/lib/python2.7/site-packages/rest_framework/compat.py", line 222, in generic
    return self.request(**r)
  File "/Users/user/dev/venv/lib/python2.7/site-packages/rest_framework/test.py", line 157, in request
    return super(APIClient, self).request(**kwargs)
  File "/Users/user/dev/venv/lib/python2.7/site-packages/rest_framework/test.py", line 109, in request
    request = super(APIRequestFactory, self).request(**kwargs)
  File "/Users/user/dev/venv/lib/python2.7/site-packages/django/test/client.py", line 444, in request
    six.reraise(*exc_info)
  File "/Users/user/dev/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 137, in get_response
    response = response.render()
  File "/Users/user/dev/venv/lib/python2.7/site-packages/django/template/response.py", line 105, in render
    self.content = self.rendered_content
  File "/Users/user/dev/venv/lib/python2.7/site-packages/rest_framework/response.py", line 59, in rendered_content
    ret = renderer.render(self.data, media_type, context)
  File "/Users/user/dev/venv/lib/python2.7/site-packages/rest_framework/renderers.py", line 333, in render
    form = data.serializer
AttributeError: 'NoneType' object has no attribute 'serializer'

I have almost the same issue with views based on mixins.ListModelMixin - it works fine, but raises exception during testing.

View definition:

class ListTestView(mixins.ListModelMixin, generics.GenericAPIView):

    pagination_class = paginators.StandardResultsSetPagination

    def get(self, request, format=None, *args, **kwargs):
        self.serializer_class = TestListSerializer
        self.queryset = Client.objects.all()
        return self.list(request, *args, **kwargs)

Calling this view from the test client:

print client.get('/api/v1/client/list/', format='json')

raises this exception:

Internal Server Error: /api/v1/client/list/
Traceback (most recent call last):
  File "/Users/user/dev/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 137, in get_response
    response = response.render()
  File "/Users/user/dev/venv/lib/python2.7/site-packages/django/template/response.py", line 105, in render
    self.content = self.rendered_content
  File "/Users/user/dev/venv/lib/python2.7/site-packages/rest_framework/response.py", line 59, in rendered_content
    ret = renderer.render(self.data, media_type, context)
  File "/Users/user/dev/venv/lib/python2.7/site-packages/rest_framework/renderers.py", line 333, in render
    form = data.serializer
AttributeError: 'OrderedDict' object has no attribute 'serializer'
Traceback (most recent call last):
  File "/Users/user/dev/managerv2/tests.py", line 22, in <module>
    print client.get('/api/v1/client/golden_state/list/1/', format='json')
  File "/Users/user/dev/venv/lib/python2.7/site-packages/rest_framework/test.py", line 160, in get
    response = super(APIClient, self).get(path, data=data, **extra)
  File "/Users/user/dev/venv/lib/python2.7/site-packages/rest_framework/test.py", line 86, in get
    return self.generic('GET', path, **r)
  File "/Users/user/dev/venv/lib/python2.7/site-packages/rest_framework/compat.py", line 222, in generic
    return self.request(**r)
  File "/Users/user/dev/venv/lib/python2.7/site-packages/rest_framework/test.py", line 157, in request
    return super(APIClient, self).request(**kwargs)
  File "/Users/user/dev/venv/lib/python2.7/site-packages/rest_framework/test.py", line 109, in request
    request = super(APIRequestFactory, self).request(**kwargs)
  File "/Users/user/dev/venv/lib/python2.7/site-packages/django/test/client.py", line 444, in request
    six.reraise(*exc_info)
  File "/Users/user/dev/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 137, in get_response
    response = response.render()
  File "/Users/user/dev/venv/lib/python2.7/site-packages/django/template/response.py", line 105, in render
    self.content = self.rendered_content
  File "/Users/user/dev/venv/lib/python2.7/site-packages/rest_framework/response.py", line 59, in rendered_content
    ret = renderer.render(self.data, media_type, context)
  File "/Users/user/dev/venv/lib/python2.7/site-packages/rest_framework/renderers.py", line 333, in render
    form = data.serializer
AttributeError: 'OrderedDict' object has no attribute 'serializer'

Any suggestions? Many thanks!

Aucun commentaire:

Enregistrer un commentaire