2023.04.20

swagger 세팅하기

drf-yasg 패키지를 설치한다.

pipenv install drf-yasg 또는 pip install drf-yasg

Screen Shot 2023-04-20 at 5 24 32 PM


settings.py 에 INSTALLED_APPS 에 drf_yasg 를 추가한다.

INSTALLED_APPS = [
    ...
    "drf_yasg",
]


여기까지 완료 되었다면, 루트에 해당하는 url.py에 다음과 같이 작성해줍니다.

from rest_framework import permissions
from drf_yasg.views import get_schema_view
from drf_yasg import openapi

schema_view = get_schema_view(
    openapi.Info(
        title="development discovery",
        default_version="",
        description="API Docs",
        terms_of_service="https://www.google.com/policies/terms/",
    ),
    public=True,
    permission_classes=[permissions.AllowAny],
)

urlpatterns = [
		...
    path(r"swagger(?P<format>\.json|\.yaml)", schema_view.without_ui(cache_timeout=0), name="schema-json"),
    path(r"swagger", schema_view.with_ui("swagger", cache_timeout=0), name="schema-swagger-ui"),
    path(r"redoc", schema_view.with_ui("redoc", cache_timeout=0), name="schema-redoc-v1"),
]

이런식으로 나오게 된다.

Screen Shot 2023-04-20 at 6 14 56 PM



Leave a comment