Django rest framework drf_yasg文档参数未显示
我正在使用drf_yasg对使用django框架构建的API进行自文档化,但是没有任何端点的参数显示出来。我这样做的方式是以JSON格式在http请求体内传递参数。然后我从request.data读取参数。下面是其中一个视图的示例,其中它将“id”作为请求主体中的参数Django rest framework drf_yasg文档参数未显示,django-rest-framework,swagger,drf-yasg,Django Rest Framework,Swagger,Drf Yasg,我正在使用drf_yasg对使用django框架构建的API进行自文档化,但是没有任何端点的参数显示出来。我这样做的方式是以JSON格式在http请求体内传递参数。然后我从request.data读取参数。下面是其中一个视图的示例,其中它将“id”作为请求主体中的参数 @api_view(['POST']) @permission_classes([IsAuthenticated]) def get_availability_by_schoolid(request): if reques
@api_view(['POST'])
@permission_classes([IsAuthenticated])
def get_availability_by_schoolid(request):
if request.user.is_donor:
try:
#Get the existing slots and remove them to replace with new slots
slots = SchoolSlot.objects.filter(school__id=request.data["id"]).values('day','am','pm')
availability = process_availability(slots)
availabilityslotserializer = SchoolSlotSerializer(availability)
return Response(availabilityslotserializer.data, status=status.HTTP_200_OK)
except Exception as e:
print(e)
return Response(ResponseSerializer(GeneralResponse(False,"Unable to locate school")).data, status=status.HTTP_400_BAD_REQUEST)
else:
return Response(ResponseSerializer(GeneralResponse(False,"Not registered as a donor")).data, status=status.HTTP_400_BAD_REQUEST)
这将显示在swagger文档中,如下所示:
我一直在看文档,它建议我可能需要添加一个带有手动参数的装饰器,但我不知道该怎么做,因为我可以找到的示例是get查询参数或路径参数。我在下面添加了decorator,它支持对文档的响应
@swagger_auto_schema(method='post', responses={200: SchoolSlotSerializer,400: 'Bad Request'})
这方面的请求主体示例如下:
{ "id": 43 }
更新:
我似乎从以下装饰师那里得到了一些东西:
@swagger_auto_schema(method='post', request_body=openapi.Schema(
type=openapi.TYPE_OBJECT,
properties={
'id': openapi.Schema(type=openapi.TYPE_INTEGER, description='Donor ID')
}),
responses={200: SchoolSlotSerializer,400: 'Bad Request'})
@swagger_auto_schema(method='post', request_body=openapi.Schema(
type=openapi.TYPE_OBJECT,
properties={
'id': openapi.Schema(type=openapi.TYPE_INTEGER, description='Donor ID')
}),
responses={200: SchoolSlotSerializer,400: 'Bad Request'})
这将记录参数。现在,我对另一个端点所做的是,它将以下JSON作为输入,我将如何添加它
{
"availability": {
"mon": {
"AM": true,
"PM": false
},
"tue": {
"AM": false,
"PM": false
},
"wed": {
"AM": false,
"PM": false
},
"thu": {
"AM": true,
"PM": true
},
"fri": {
"AM": false,
"PM": false
},
"sat": {
"AM": false,
"PM": false
},
"sun": {
"AM": true,
"PM": false
}
}
}
您可以为此使用ModelSerializer:
@swagger_auto_schema(method='post', request_body=ModelSerializer)
我相信这正是它发挥作用的原因: 如果有更好的办法,请告诉我