Search code examples
djangodjango-rest-frameworkdjango-viewsdrf-spectacular

Warnings for a View without parameters in drf-spectacular


There are a problem with drf-spectacular. I have a View, something like this:

@extend_schema()
class SomeView(APIView):
    def get(self, request):
        return JsonResponse("Hello world!")

in other words, a View that does not accept parameters. And when generating the schema, I get a warning in the console:

Error [SomeView]: unable to guess serializer. This is graceful fallback handling for APIViews. Consider using GenericAPIView as view base class, if view is under your control. Either way you may want to add a serializer_class (or method). Ignoring view for now

How to make it so that there is no warning and the scheme is generated correctly?

UPD: I forgot to say extend_schema decorator is used (change code block). I tried different options, for example: use an empty inline_serializer with fields={} or experiment with OpenApiParameter


Solution

  • You can use the extend_schema(...) decorator to generate the schema

    from rest_framework.views import APIView
    from rest_framework.response import Response
    from drf_spectacular.utils import extend_schema
    
    
    class SomeView(APIView):
        @extend_schema(request=None, responses={200: {"type": "string"}})
        def get(self, request):
            return Response("Hello world!")