drf-spectacular: Specify empty payload using @extend_schema


Consider that I have a simple view as,

# serializers.py
class EmptyPayloadResponseSerializer(serializers.Serializer):
    detail = serializers.CharField()

# views.py
from rest_framework.views import APIView
from rest_framework.response import Response
from drf_spectacular.utils import extend_schema
from .serializers import EmptyPayloadResponseSerializer

class EmptyPayloadAPI(APIView):
    def post(self, request, *args, **kwargs):
        # some actions
        return Response(data={"detail": "Success"})

When I render the schema, I have got the following error response,

Error #0: EmptyPayloadAPI: unable to guess serializer. This is graceful fallback handling for APIViews. Consider using GenericAPIView as view base class, if view is under your control. Ignoring view for now.

So, how can I tell to @extend_schema decorator that I’m intended to pass nothing as payload?


Settings request=None in the @extend_schema(...) decorator will do the trick!!!

class EmptyPayloadAPI(APIView):
    @extend_schema(request=None, responses=EmptyPayloadResponseSerializer)
    def post(self, request, *args, **kwargs):
        # some actions
        return Response(data={"detail": "Success"})
Leave a Reply

Your email address will not be published. Required fields are marked *