pythondjangodjango-rest-frameworkswagger-uidrf-spectacular

How to show required request body using django rest framework + drf_spectacular + swagger


I started using drf_spectacular and swagger recently, I have a an API endpoint that expects request body with field in it, however I can't seem to understand how to reveal this in the UI.

my view:

    @extend_schema(
    responses={200: TripSerializer},
)
def get(self, request: Request) -> Response:
    trip = TripService.get_trip(trip_id=request.data.get("trip_id"))
    if trip is None:
        return Response(status=status.HTTP_404_NOT_FOUND)
    trip_serializer = TripSerializer(trip)
    return Response(trip_serializer.data, status=status.HTTP_200_OK)

my request body:

{
"trip_id": "1"
}

my swagger UI ps

enter image description here

will appriciate any help, Thanks


Solution

  • You can add it to the schema:

    @extend_schema(
            request=InputSerializer,
            responses={200: TripSerializer}
        ) 
    

    If you don't have already the InputSerializer, just create it:

    class InputSerializer(serializers.Serializer): 
       trip_id = seriarlizer.IntegerField()