drf-spectacular:使用@extend_schema 指定空负载
drf-spectacular: Specify empty payload using @extend_schema
考虑到我有一个简单的观点,
# 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"})
当我呈现架构时,我收到以下错误响应,
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.
那么,我如何告诉 @extend_schema
装饰器我不打算将任何内容作为有效负载传递?
在 @extend_schema(...)
装饰器中设置 request=None
就可以了!!!
class EmptyPayloadAPI(APIView):
@extend_schema(<b>request=None</b>, responses=EmptyPayloadResponseSerializer)
def post(self, request, *args, **kwargs):
# some actions
return Response(data={"detail": "Success"})
考虑到我有一个简单的观点,
# 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"})
当我呈现架构时,我收到以下错误响应,
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.
那么,我如何告诉 @extend_schema
装饰器我不打算将任何内容作为有效负载传递?
在 @extend_schema(...)
装饰器中设置 request=None
就可以了!!!
class EmptyPayloadAPI(APIView):
@extend_schema(<b>request=None</b>, responses=EmptyPayloadResponseSerializer)
def post(self, request, *args, **kwargs):
# some actions
return Response(data={"detail": "Success"})