Skip to content

Swagger (drf-yasg)

GET

from drf_yasg.utils import swagger_auto_schema
from drf_yasg import openapi

@swagger_auto_schema(
    method="get",
    manual_parameters=[
        Parameter("secret", openapi.IN_HEADER, type="string"),
        Parameter("query_param", openapi.IN_QUERY, type="string"),
    ],
    operation_description="",
)

POST

from drf_yasg.utils import swagger_auto_schema
from drf_yasg import openapi
@swagger_auto_schema(
    method="post",  # remove this for class based
    request_body=openapi.Schema(
        type=openapi.TYPE_OBJECT,
        required=["geo", "month", "year"],
        properties={
            "name": openapi.Schema(type=openapi.TYPE_STRING, description="example: "),
        },
    ),
    responses={202: ""},
)
@api_view(["POST"])
@permission_classes((IsSuperUser,))
def post_something(request):
    name = request.data["name"]

Swagger on a class view

  • remove the method for @swagger_auto_schema
  • use the same decorator on the class

Last update: 2022-11-04