drf-yasg 用于 GET 请求的文档输入和输出序列化程序

drf-yasg document input and output serializer for GET request

我想用 drf-yasg 记录 GET 请求的输入模式和输出模式。

好像不容易啊

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

以上代码显示了 GET 参数,但以某种方式隐藏了响应模式。

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

我不能将 request_body 用于 GET 查询参数,它仅用于 post body

那么如何使用 drf-yasg 记录我的输入模式和输出模式?

您可以使用query_serializer

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

很难从官方文档中得到它。

我的 api 视图是:

    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)

我的序列化器也是 ModelSerializer:

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

不要在 drf_yasg 中为我显示参数仅 GET 视图。