DRF YASG Customizing

后端 未结 1 542
陌清茗
陌清茗 2021-02-09 16:04

I\'m trying to customize my api documentation buuild with yasg.

First off, I would like to determine the naming of my own sections, and what endpoints should be included

1条回答
  •  一整个雨季
    2021-02-09 16:30

    Currently, I'm using APIView and @swagger_auto_schema to define the documentation of my endpoint.

    In the code below, you can see how to add more information to define your endpoint. I hope it helps you

    ##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)
    

    By last, if you want to see your endpoints in groups by link, you can test commenting the line below in yours urls.py

    #urlpatterns = format_suffix_patterns(urlpatterns)
    

    Below, some screen of how you should see it

    home Get all categories Post a new category Endpoints in groups

    You can find more information in the following link: https://drf-yasg.readthedocs.io/en/stable/custom_spec.html

    0 讨论(0)
提交回复
热议问题