https://www.django-rest-framework.org/tutorial/3-class-based-views/
Class-base views의 장점은
1. 공통의 기능을 재사용할 수 있다.
2. code를 "DRY"하게 유지할 수 있다.
* DRY(Don't Repeat Yourself): 반복[중복]을 줄이는 software pattern
class-based views를 사용하여 API를 재작성
이전의 function based views를 class-based views로 바꿔보자
snippets/views.py
from snippets.models import Snippet
from snippets.serializers import SnippetSerializer
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from django.http import Http404
class SnippetList(APIView):
"""
List all code snippets, or create new snippet.
"""
def get(self, request, format=None):
snippets = Snippet.objects.all()
serializer = SnippetSerializer(snippets, many=True)
return Response(serializer.data)
def post(self, request, format=None):
serializer = SnippetSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.data, status=status.HTTP_400_BAD_REQUEST)
class SnippetDetail(APIView):
"""
Retrieve, update or delete a code snippet.
"""
def get_object(self, pk):
try:
return Snippet.objects.get(pk=pk)
except Snippet.DoesNotExist:
raise Http404
def get(self, request, pk, format=None):
snippet = self.get_object(pk)
serializer = SnippetSerializer(snippet)
return Response(serializer.data)
def put(self, request, pk, format=None):
snippet = self.get_object(pk)
serializer = SnippetSerializer(snippet, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def delete(self, request, pk, format=None):
snippet = self.get_object(pk)
snippet.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
urls도 바꿔보자
snippets/urls.py
from django.urls import path
from snippets import views
from rest_framework.urlpatterns import format_suffix_patterns
urlpatterns = [
path('snippets/', views.SnippetList.as_view()),
path('snippets/<int:pk>/', views.SnippetDetail.as_view()),
]
urlpatterns = format_suffix_patterns(urlpatterns) # format suffix
이전과 같이 잘 동작하는지 확인하고 넘어가자
mixins 사용하기
class-based views는 확장성 면에서 function base보다 유리한데,
이런 장점을 극대화 시켜주는 것이 바로 mixins class이다.
mixin classes를 사용해서 views를 재구성해보자
snippets/views.py
from snippets.models import Snippet
from snippets.serializers import SnippetSerializer
from rest_framework import mixins
from rest_framework import generics
class SnippetList(mixins.ListModelMixin, mixins.CreateModelMixin, generics.GenericAPIView):
queryset = Snippet.objects.all()
serializer_class = SnippetSerializer
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)
class SnippetDetail(mixins.RetrieveModelMixin, mixins.UpdateModelMixin, mixins.DestroyModelMixin, generics.GenericAPIView):
queryset = Snippet.objects.all()
serializer_class = SnippetSerializer
def get(self, request, *args, **kwargs):
return self.retrieve(request, *args, **kwargs)
def put(self, request, *args, **kwargs):
return self.update(request, *args, **kwargs)
def delete(self, request, *args, **kwargs):
return self.destroy(request, *args, **kwargs)
여기서 view는 GenericAPIView를 통해서 빌드했고,
mixins를 더함으로써 .list(), .create() 와 같은 동작을 제공할 수 있게 되는 것이다.
Generic class-based views
사실, REST framework는 이미 mixed-in generic views를 제공함으로써 보다 간편하게 코드를 작성할 수 있게 한다.
from snippets.models import Snippet
from snippets.serializers import SnippetSerializer
from rest_framework import generics
class SnippetList(generics.ListCreateAPIView):
queryset = Snippet.objects.all()
serializer_class = SnippetSerializer
class SnippetDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = Snippet.objects.all()
serializer_class = SnippetSerializer
views가 엄청 간단해졌다!
'Python > Django' 카테고리의 다른 글
[Django REST framework] 5. Relationships & Hyperlinked APIs (0) | 2020.04.11 |
---|---|
[Django REST framework] 4. Authentication & Permissions (0) | 2020.04.10 |
[Django REST framework] 2. Requests and Responses (0) | 2020.04.10 |
[Django REST framework] 1. Serialization (0) | 2020.04.08 |
[Django Channels 2.4.0] Building simple Chat Server / 채팅 서버 구현 튜토리얼 part.3 Rewrite Chat Server as Asynchronous (0) | 2020.04.06 |