Skip to content

Instantly share code, notes, and snippets.

@KimSoungRyoul
Last active April 18, 2021 04:05
Show Gist options
  • Save KimSoungRyoul/aef3ec4edda1b0815ed9ee17f0503d9d to your computer and use it in GitHub Desktop.
Save KimSoungRyoul/aef3ec4edda1b0815ed9ee17f0503d9d to your computer and use it in GitHub Desktop.
from django.contrib.auth.models import User
from django.http import HttpResponse
from drf_spectacular.types import OpenApiTypes
from drf_spectacular.utils import OpenApiExample, OpenApiParameter, extend_schema_view
from drf_spectacular.utils import extend_schema
from rest_framework.decorators import action
from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from rest_framework import serializers, status
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
depth = 1
fields = "__all__"
class CustomUserSerializer(serializers.ModelSerializer):
user_type = serializers.CharField(help_text="회원의 유형값을 받습니다.", default="customer")
class Meta:
model = User
fields = "__all__"
@extend_schema_view(
list=extend_schema(summary="이런식으로 class레벨 데코레이터로 문서 커스터마이징 가능하다.", tags=["사용자"]),
i_am_custom_api=extend_schema(
summary="@action API도 마찬가지로 class 데코레이터로 문서 커스터마이징 가능하다.",
tags=["사용자"],
request=CustomUserSerializer,
responses={status.HTTP_200_OK: CustomUserSerializer},
),
)
class UserViewSet(ModelViewSet):
queryset = ShoppingMallUser.objects.all()
serializer_class = UserSerializer
@extend_schema(
tags=["사용자"],
summary="method레벨 데코레이터도 가능",
parameters=[
OpenApiParameter(name="a_param", description="QueryParam1 입니다.", required=False, type=str),
OpenApiParameter(
name="date_param",
type=OpenApiTypes.DATE,
location=OpenApiParameter.QUERY,
description="Filter by release date",
examples=[
OpenApiExample(
"이것은 Query Parameter Example입니다.",
summary="short optional summary",
description="longer description",
value="1993-08-23",
),
],
),
],
examples=[
OpenApiExample(
request_only=True,
summary="이거는 Request Body Example입니다.",
name="success_example",
value={
"username": "root",
"password": "django_1234",
"first_name": "성렬",
"last_name": "김",
"email": "user@example.com",
},
),
],
)
def create(self, request: Request, *args, **kwargs) -> Response:
response: HttpResponse = super().create(request, *args, **kwargs)
return response
@action(
detail=False, url_path="custom-action-api",
)
def i_am_custom_api(self, request: Request, *args, **kwargs):
return Response(data={"hi": "i am custom api"})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment