DRF-YASG:为GET请求文档编写输入和输出序列化器

9
我愿意用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 请求体

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

2个回答

7

-1

我的 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 视图中为我显示参数。


这不是一个答案。 - Elias Prado

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接