swagger @method_decorator, @swagger_auto_schema
Language/Python

swagger @method_decorator, @swagger_auto_schema

뉴비뉴 2020. 2. 25.

swagger란

자동으로 API 목록과 각 API의 Request Body, Query Parameter를 문서화해주며,

바로 Postman과 같이 API를 테스트해 볼 수 있는 많은 프로젝트에서 사용하는

검증된 문서화 도구입니다.

 

 

class PersonViewSet(viewsets.ReadOnlyModelViewSet):

 

ReadOnlyModelViewSet을 따라가보면 mixins.RetrieveModelMixin, ListModelMixin을

Mixin 하여 사용하고 있는 것을 확인할 수 있다.

그래서 PersonViewSet에 @method_decorator를 사용하려면 

list와 retrieve와 관련 된 것만 사용할 수 있다.

왜냐하면 클래스로 작성되어 있기 때문이다.

 

아래 링크에 사용법을 자세히 설명해주고 있으니 확인해보면 좋을 거 같습니다.

https://ssungkang.tistory.com/entry/Django-FBV-%EC%99%80-CBV-%EC%9D%98-decorators-%EC%82%AC%EC%9A%A9%EB%B2%95

@method_decorator(name='list', decorator=swagger_auto_schema(
    operation_id='사람 list',
    operation_description='사람 리스트 반환',
    tags=['Person'],
))
@method_decorator(name='retrieve', decorator=swagger_auto_schema(
    operation_id='사람 retrieve',
    operation_description='사람 정보 반환',
    tags=['Person'],
))
class PersonViewSet(viewsets.ReadOnlyModelViewSet):

그렇다면 list와 retieve 외에 update에도 따로 사용하고 싶은 함수가 있다고 가정하고,

@swagger_auto_schema(
    method='patch',
    operation_id='사람 정보 업데이트',
    operation_description='사람 정보를 업데이트합니다.',
    tags=['Person'],
)
@api_view(['PATCH'])

def person_update(self):

 

아래 링크는 swagger_auto_schema를 잘 설명해주고 있어 참고하면 좋을거같다.

https://medium.com/towncompany-engineering/%EC%B9%9C%EC%A0%88%ED%95%98%EA%B2%8C-django-rest-framework-api-%EB%AC%B8%EC%84%9C-%EC%9E%90%EB%8F%99%ED%99%94%ED%95%98%EA%B8%B0-drf-yasg-c835269714fc

댓글

💲 추천 글