2

考虑到我有一个简单的观点,

# 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):
    @extend_schema(responses=EmptyPayloadResponseSerializer)
    def post(self, request, *args, **kwargs):
        # some actions
        return Response(data={"detail": "Success"})

当我渲染架构时,我得到了以下错误响应,

错误 #0:EmptyPayloadAPI:无法猜测序列化程序。这是对 APIViews 的优雅回退处理。如果视图在您的控制之下,请考虑使用 GenericAPIView 作为视图基类。暂时忽略视图。

那么,我如何告诉@extend_schema装饰器我不打算将任何内容作为有效负载传递?

4

1 回答 1

3

request=None装饰器中的设置@extend_schema(...)可以解决问题!!!

class EmptyPayloadAPI(APIView):
    @extend_schema(request=None, responses=EmptyPayloadResponseSerializer)
    def post(self, request, *args, **kwargs):
        # some actions
        return Response(data={"detail": "Success"})
于 2021-05-22T12:09:33.233 回答