How to add own Parameters and Response values in the drf-yasg library for swagger documentation?
Asked Answered
R

2

5

I use the drf-yasg library to generate project documentation for the developer front-ends. I can't figure out how to add my data to the fields Parameters and Responses

For example, this class

class VendorsCreateView(APIView):
    """
    :param:
            data = {
            "vendor_name": "TestName",
            "country": "Belarus",
            "nda": "2020-12-12",
            "parent": "",
            "contacts": [{"contact_name": "Mrk", "phone": "2373823", "email": "[email protected]"},
                         {"contact_name": "Uio", "phone": "34567", "email": "[email protected]"}
                         ]
        }
    :return: swagger name
    """

But I don't exactly get what I want enter image description here

How do I add these parameters?

Retroflexion answered 17/3, 2020 at 9:54 Comment(0)
A
4

@swagger_auto_schema decorator is what you are looking for.

Angloamerican answered 7/4, 2020 at 10:0 Comment(2)
Thank you very much, that question was still relevant. Could you tell how to add a field parameters? I'm trying this way but nothing is displayed (both operation_description and operation_description work). @swagger_auto_schema(operation_description="partial_update description override", responses={404: 'slug not found'}, parameters="test")Retroflexion
Use request_body argument with a serializer or a schema. Checkout testproj code on drf-yasg GitHub, e.g. github.com/axnsan12/drf-yasg/blob/… lines 26 and 49.Wojcik
R
9

Maybe it will help someone who wants extra parameter in GET-request - you should use argument "manual_parameters" in drf-yasg, e.g. field you want to order by to:

order_by = openapi.Parameter('order_by', openapi.IN_QUERY,
                             description="field you want to order by to",
                             type=openapi.TYPE_STRING)

@swagger_auto_schema(manual_parameters=[order_by])
def list(self, request, *args, **kwargs):

swagger: enter image description here

Runion answered 22/8, 2022 at 14:17 Comment(0)
A
4

@swagger_auto_schema decorator is what you are looking for.

Angloamerican answered 7/4, 2020 at 10:0 Comment(2)
Thank you very much, that question was still relevant. Could you tell how to add a field parameters? I'm trying this way but nothing is displayed (both operation_description and operation_description work). @swagger_auto_schema(operation_description="partial_update description override", responses={404: 'slug not found'}, parameters="test")Retroflexion
Use request_body argument with a serializer or a schema. Checkout testproj code on drf-yasg GitHub, e.g. github.com/axnsan12/drf-yasg/blob/… lines 26 and 49.Wojcik

© 2022 - 2024 — McMap. All rights reserved.