drf-yasg document input and output serializer for GET request
Asked Answered
P

2

10

I'd like to document input schemas and output schemas for GET request with drf-yasg.

It doesn't seem to be easy.

     @swagger_auto_schema(
         manual_parameters=[
             openapi.Parameter('cart_id', in_=openapi.IN_QUERY,
                               type=openapi.TYPE_INTEGER)
         ])

The above code shows the GET parameter, but somehow hides the response schema.

@swagger_auto_schema(methods=['put', 'post'], request_body=UserSerializer)

I can't use request_body for GET query parameters, it's only for post body

So How do I document my input schema and output schema with drf-yasg ?

Potpie answered 21/9, 2019 at 3:23 Comment(0)
P
8

You can use query_serializer

Found it https://medium.com/@arjunsinghy96/customised-api-documentation-for-django-rest-framework-projects-using-drf-yasg-d6db9ba5cff3

was hard to get it from the official doc.

Potpie answered 21/9, 2019 at 3:45 Comment(0)
P
-1

my api View is :

    class ProductListView(APIView):
        """
            get 1 or list of products for show to users
        """
    
        serializer_class = ProductGetSerializer
        permission_classes = (
            AllowAny,
        )
    
        def get(self, request, product_id=None):
            if product_id is not None:
                product = get_object_or_404(Product.confirmed, pk=product_id)
                srz_data = self.serializer_class(instance=product)
                return Response(data=srz_data.data, status=status.HTTP_200_OK)
    
            products = Product.confirmed.all()
            srz_data = self.serializer_class(instance=products, many=True)
            return Response(data=srz_data.data, status=status.HTTP_200_OK)

also my serializer is ModelSerializer:

class ProductGetSerializer(serializers.ModelSerializer):
    class Meta:
        model = Product
        fields = (
            'id',
            'name',
            'image',
            'category',
            'description',
            'price',
            'stock',
        )

don't show for me params in drf_yasg only GET views.

Pram answered 4/7, 2021 at 13:19 Comment(1)
This is not an answer.Peptidase

© 2022 - 2024 — McMap. All rights reserved.