因此,我使用drf-spectacular
记录以下代码:
from rest_framework import response
from rest_framework.decorators import api_view, permission_classes
from rest_framework.response import Response
from rest_framework.permissions import AllowAny
from rest_framework import status
from drf_spectacular.utils import extend_schema, OpenApiParameter
def passcode_generator:
return 0 # placeholder
@extend_schema(
parameters=[
OpenApiParameter(name="callsign", required=True, type=str),
],
description="Get an APRS-IS passcode for a given callsign",
)
@api_view(["POST"])
@permission_classes([AllowAny])
def get_passcode(request):
callsign = request.data.get("callsign", None)
if callsign is None:
raise Response(
{"error": "Missing callsign"}, status=status.HTTP_400_BAD_REQUEST
)
return Response({"passcode": passcode_generator(callsign)})
我不明白的是如何记录响应。也就是说,在drf_spectacular.utils
中有OpenApiResponse
,但文档非常少。我如何在这个系统中记录我的API的响应?
2条答案
按热度按时间2hh7jdfx1#
如果你没有
Serializer
,你可以使用inline_serializer
:lhcgjxsq2#
与此同时,我发现了一个令人满意的答案,也让自动文档生成器感到高兴:
我创建了几个序列化器:
在视图中(注意最后一行之前的注解):