DRF YASG定制

问题描述 投票:2回答:1

我正在尝试自定义使用yash构建的api文档。

首先,我想确定自己部分的命名,以及本节中应包含哪些端点。似乎段的命名基于不属于最长公共前缀的第一个前缀,例如:

如果我们有url api / v1 / message和api / v1 / test,那么这些部分将被命名为message和test。有没有办法让我确定本节的自定义命名?

此外,每个部分的介绍都是空的,我如何在这里添加文字? How to add text here?

最后但并非最不重要的是,Stripe有这些惊人的截面分隔符,我如何在drf yasg中添加这些。

section dividers

django django-rest-framework documentation openapi drf-yasg
1个回答
2
投票

目前,我正在使用API​​View和@swagger_auto_schema来定义我的端点的文档。

在下面的代码中,您可以看到如何添加更多信息来定义端点。我希望它对你有所帮助

##serializers.py

class CategorySerializer(serializers.ModelSerializer):
    """
    Serializing Categories 
    """
    class Meta:
        model = Category
        fields = [
            'id', 'name', 'slug'
        ]
        read_only_fields = [
           'slug', 
        ]


##views.py

username_param = openapi.Parameter('username', in_=openapi.IN_QUERY, description='Username',
                                type=openapi.TYPE_STRING)
email = openapi.Parameter('email', in_=openapi.IN_QUERY, description='Email',
                                type=openapi.TYPE_STRING)  
category_response = openapi.Response('response description', CategorySerializer)    

class CategoryList(APIView):
    permission_classes = [AllowAny]

    @swagger_auto_schema(
        manual_parameters=[username_param, email],
        query_serializer=CategorySerializer,
        responses = {
            '200' : category_response,
            '400': 'Bad Request'
        },        
        security=[],
        operation_id='List of categories',
        operation_description='This endpoint does some magic',
    )
    def get(self, request, format=None):
        """
        GET:
        Return a list of all the existing categories.
        """
        categories = Category.objects.all()
        serializer = CategorySerializer(categories, many=True)
        return Response(serializer.data)


    @swagger_auto_schema(
        request_body=CategorySerializer,
        query_serializer=CategorySerializer,
        responses={
            '200': 'Ok Request',
            '400': "Bad Request"
        },
        security=[],
        operation_id='Create category',
        operation_description='Create of categories',
    )
    def post(self, request, format=None):
        """
        POST:
        Create a new category instance.
        """
        serializer = CategorySerializer(data=request.data)
        if serializer.is_valid():
            serializer.save(created_by=self.request.user)
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

最后,如果您想通过链接分组查看您的终端,您可以在您的urls.py中测试以下行的注释

#urlpatterns = format_suffix_patterns(urlpatterns)

下面是一些你应该如何看待它的屏幕

[home][1]
[Get all categories][2]
[Post a new category][3]
[Endpoints in groups][4]

You can find more information in the link below

https://drf-yasg.readthedocs.io/en/stable/custom_spec.html

  [1]: https://i.stack.imgur.com/iwsnB.png
  [2]: https://i.stack.imgur.com/R7f1q.png
  [3]: https://i.stack.imgur.com/1ia9F.png
  [4]: https://i.stack.imgur.com/Gwckf.png
© www.soinside.com 2019 - 2024. All rights reserved.