Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SchemaGenerator fails with message "object has no attribute 'get_serializer_class'" #4265

Closed
geekashu opened this issue Jul 15, 2016 · 9 comments

Comments

@geekashu
Copy link

geekashu commented Jul 15, 2016

Hi,

I just upgraded to 3.4.0 of django-rest-framework. In my application i have a API view which doesn't have a GET method implemented only POST is there.

Now i am trying to auto generate the schema using the tutorial given. Here is my code for the schema view.

from rest_framework.decorators import api_view, renderer_classes
from rest_framework import renderers, schemas

@api_view()
@renderer_classes([renderers.CoreJSONRenderer, ])
def schema_view(request):
    generator = schemas.SchemaGenerator(title='Bookings API')
    return generator.get_schema()

Now when i try to view this schema i am getting this error.

Traceback (most recent call last):
  File "/home/ashish/Env/backend/lib/python3.4/site-packages/django/core/handlers/base.py", line 149, in get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/ashish/Env/backend/lib/python3.4/site-packages/django/core/handlers/base.py", line 147, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/ashish/Env/backend/lib/python3.4/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File "/home/ashish/Env/backend/lib/python3.4/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/ashish/Env/backend/lib/python3.4/site-packages/rest_framework/views.py", line 466, in dispatch
    response = self.handle_exception(exc)
  File "/home/ashish/Env/backend/lib/python3.4/site-packages/rest_framework/views.py", line 463, in dispatch
    response = handler(request, *args, **kwargs)
  File "/home/ashish/Env/backend/lib/python3.4/site-packages/rest_framework/decorators.py", line 52, in handler
    return func(*args, **kwargs)
  File "/home/ashish/Projects/backend/oyster/config/swagger.py", line 7, in schema_view
    generator = schemas.SchemaGenerator(title='Bookings API')
  File "/home/ashish/Env/backend/lib/python3.4/site-packages/rest_framework/schemas.py", line 74, in __init__
    self.endpoints = self.get_api_endpoints(patterns)
  File "/home/ashish/Env/backend/lib/python3.4/site-packages/rest_framework/schemas.py", line 128, in get_api_endpoints
    prefix=path_regex
  File "/home/ashish/Env/backend/lib/python3.4/site-packages/rest_framework/schemas.py", line 121, in get_api_endpoints
    link = self.get_link(path, method, callback)
  File "/home/ashish/Env/backend/lib/python3.4/site-packages/rest_framework/schemas.py", line 196, in get_link
    fields += self.get_serializer_fields(path, method, callback, view)
  File "/home/ashish/Env/backend/lib/python3.4/site-packages/rest_framework/schemas.py", line 256, in get_serializer_fields
    serializer_class = view.get_serializer_class()
AttributeError: 'LogoutView' object has no attribute 'get_serializer_class'

Here is my LogoutView class..

class LogoutView(APIView):
    permission_classes = (AllowAny,)

    def post(self, request):
        return self.logout(request)

    def logout(self, request):
        try:
            request.user.auth_token.delete()
        except (AttributeError, ObjectDoesNotExist):
            pass

        logout(request)

        log.info('Logout Successful | %s' % request.user)
        content = {'success': settings.USERS_LOGOUT_MSG}
        return Response(content, status=status.HTTP_200_OK)

Am i missing something? What needs to be done to auto generate the schema?

@decentral1se
Copy link
Contributor

decentral1se commented Jul 18, 2016

I am also seeing a similar error. Except, that the call to view.get_serializer_class from my ModelViewSet gets into the get_serializer_class method and fails when it doesn't have the request attribute.

Is there any update on this one? I will try to write a failing test case ...

@geekashu

Have you defined a serializer for this view? You should either have a serializer_class = X or override get_serializer_class() ?

@xordoquy
Copy link
Collaborator

xordoquy commented Jul 18, 2016

The OP mentions an APIView and I'm not sure SchemaGenerator do work with them which is why I removed the bug status.

@lwm test case would definitively help get this fixed faster.

@geekashu
Copy link
Author

geekashu commented Jul 18, 2016

@lwm No i haven't defined a serializer for this view.

@xordoquy SchemaGenerator should work with all APIView as per this comment

@MadWombat
Copy link

MadWombat commented Jul 18, 2016

I am getting the same error on my function based views. My view looks sort of like this

@api_view(['GET', 'POST'])
@authentication_classes((CustomAuthentication, ))
@permission_classes((CustomPermission, ))
def get_all(request):
    return Response(CustomSerializer(CustomModel.objects.all(), many=True).data)

Before you ask, I have to support both GET and POST for legacy reasons.

@sasha0
Copy link
Contributor

sasha0 commented Jul 18, 2016

Issue is easy to re-recreate - basically when SchemaGenerator iterates through API views and any of them derived from APIView, simply because it does not have this method.

@sasha0
Copy link
Contributor

sasha0 commented Jul 19, 2016

I've added PR with the test and suggested fix.

@xordoquy
Copy link
Collaborator

xordoquy commented Jul 19, 2016

Thanks, this will definitively help

@swehba
Copy link

swehba commented Nov 19, 2018

Not sure why this issue was closed. I also have this problem, and it appears that it occurs for any view that implements get_serializer_class if, within that method, you access self.request (e.g., if self.request.method == 'GET':...). In that situation, self.request is set to None.

@Ambitiont109
Copy link

Ambitiont109 commented Mar 23, 2022

I also have the same issue with Django 4.0 & Django Rest Framework 3.13.1

  File "/.../views.py", line 49, in get_serializer_class
    if self.request.method == 'GET':
AttributeError: 'NoneType' object has no attribute 'method'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants