Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading