Search code examples
pythondjangodjango-rest-frameworkdrf-yasg

"http method not bound to view" when documenting class-based view in drf_yasg


Previously, I documented my function-based views like this:

@swagger_auto_schema(
    operation_id='ChangePassword',
    methods=['POST'],
    request_body=ChangePasswordSerializer,
    responses={
        '200': 'empty response body',
    })
def change_password(request):
    # code here

We then switched our views to class-based, so I simply copy-pasted the documentation decorator over to the post method:

class UserChangePasswordView(APIView):
    @swagger_auto_schema(
        operation_id='ChangePassword',
        methods=['POST'],
        request_body=ChangePasswordSerializer,
        responses={
            '200': 'empty response body',
        })
    def post(self, request):
        # code here 

However, on running this decorator, drf_yasg threw the exception

File "/usr/local/lib/python3.6/site-packages/drf_yasg/utils.py", line 126, in decorator
    assert all(mth in available_methods for mth in _methods), "http method not bound to view"

What is going on? What does this error message mean?


Solution

  • Note that in source core of drf-yasg it's mentioned

    method and methods are mutually exclusive and must only be present when decorating a view method that more than one HTTP request method.

    So methods would be valid if your UserChangePasswordView.post() handled more than one method.