본문 바로가기
IT/TIL

[Django] Swagger 문서 자동화(drf-yasg)

by Better Me 2022. 3. 29.

저번 프로젝트에선 Swagger에서 API Docs를 수동으로 다 적었다.

이번엔 자동으로 만들어주는 라이브러리를 사용하기로 했다.

django-rest-swagger VS drf-yasf

Django 공식문서에서는 에서 API 문서 자동화를 위한 tool로 "django-rest-swagger"를 언급했다.

하지만, 구글링한 결과 해당 패키지는 관리가 되지 않아 여러 오류가 있다고 했다.

그래서 drf-yasf 패키지를 사용하기로 결정!

 

라이브러리 적용하기

https://life-with-coding.tistory.com/460

 

[django] swagger로 REST API 문서 만들기

인트로 안녕하세요 :D 오늘은 장고에서 Swagger를 활용하여 REST API Doc을 만들어 보겠습니다. swagger는 프로젝트 내 많은 API를 한꺼번에 문서화하여 볼 수 있게 하는 패키지로, API 관리에 매우 용이합

life-with-coding.tistory.com

https://velog.io/@lu_at_log/drf-yasg-and-swagger

 

drf-yasg를 이용한 Swagger 문서 자동화

drf-yasg 라이브러리를 이용하여 Swagger 문서 자동화를 구현한 예제

velog.io

Troubleshooting

해당 글들을 참고하여 라이브러리를 다운받고 기본 설정을 완료했다.

하지만 대부분의 블로그 글들은 CBV로 구현하여 클래스 변수로 serialize_class 선언 후 serializer를 받아온다.

나의 경우 FBV로 구현하여 해당 방법을 쓸 수 없었다.

sol) https://drf-yasg.readthedocs.io/en/stable/readme.html

 

drf-yasg - Yet another Swagger generator — drf-yasg 1.20.1 documentation

Since the schema does not usually change during the lifetime of the django process, there is out of the box support for caching the schema view in-memory, with some sane defaults: caching is enabled by the cache_page decorator, using the default Django cac

drf-yasg.readthedocs.io

공식문서를 참고하여 해결!

Serializer를 하나 선언한 후, 데코레이터를 CBV 방식과 똑같이 선언하되, method를 지정하면 된다.

 

'IT > TIL' 카테고리의 다른 글

process id를 확인할 수 없을 때  (0) 2022.04.02
[Django] WSL2, Ubuntu 20.04 - mysqlclient 설치 오류  (0) 2022.04.01

댓글