I am using drf_yasg to self document my API which is built using the django-framework, but none of the parameters are displaying at all for any of the endpoints. The way I've done it is to pass the parameters within the http request body in JSON format. I then read the parameters from request.data. Below is an example of one of the views where it takes 'id' as a parameter in the body of the request.
@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)
This will display in the swagger documentation as below:
I have been looking at the documentation and it suggests that i might need to add a decorator with manual parameters, but I can't figure out how I would do that as the examples I can find are for get query parameters or path parameters. I have added the decorator below which enables the response to document.
@swagger_auto_schema(method='post', responses={200: SchoolSlotSerializer,400: 'Bad Request'})
An example of the request body for this is:
{ "id": 43 }
UPDATE:
I seem to get something with the following decorator:
@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'})
This documents the parameters. Now what I have for another endpoint is it takes the following JSON for input, how would I go about adding this?
{
"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
}
}
}
you can use your ModelSerializer for that: