我想用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记录我的输入模式和输出模式呢?
发布于 2019-09-21 11:45:52
您可以使用query_serializer
很难从官方的医生那里得到。
发布于 2021-07-04 21:19:36
我的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中显示仅获取视图的参数。
https://stackoverflow.com/questions/58037024
复制相似问题