urls.py
from django.urls import path, include
from rest_framework import routers
from .views import UserViewSet, PostViewSet, CommentViewSet
router = routers.DefaultRouter()
router.register(r'user', UserViewSet)
router.register(r'post', PostViewSet)
router.register(r'comment', CommentViewSet)
urlpatterns = [
path('', include(router.urls)),
]
views.py
from django.contrib.auth.models import User
from rest_framework import viewsets
from .serializers import UserSerializer, PostSerializer, CommentSerializer
from blog.models import Post, Comment
# ViewSets define the view behavior.
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer
class PostViewSet(viewsets.ModelViewSet):
queryset = Post.objects.all()
serializer_class = PostSerializer
class CommentViewSet(viewsets.ModelViewSet):
queryset = Comment.objects.all()
serializer_class = CommentSerializer
serializers.py
from django.contrib.auth.models import User
from rest_framework import serializers
from blog.models import Post, Comment
# Serializers define the API representation.
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ['url', 'username', 'email', 'is_staff']
class PostSerializer(serializers.ModelSerializer):
class Meta:
model = Post
# fields = '__all__'
fields = ['id', 'title', 'image', 'like', 'category']
class CommentSerializer(serializers.ModelSerializer):
class Meta:
model = Comment
fields = '__all__'
# fields = ['id', 'title', 'image', 'like', 'category']
viewsets를 사용하면 따로 CRUD를 구현할 필요 없이 한꺼번에 제공됨
하지만 그 내부에서 커스터마이징하여 사용하기가 까다로움
커스터마이징하여 사용하려면 Generic View를 사용하는 것이 유리!
Generic Views 종류 : ListAPIView, CreateAPIView, RetieveAPIView, UpdateAPIView, DestroyAPIView
구현하기 앞서 url과 그것에 매핑되는 view를 미리 정하는 것이 좋음
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('post/', views.PostListAPIView.as_view(), name='post-list'),
path('post/<int:pk>/', views.PostRetrieveAPIView.as_view(), name='post-detail'),
path('comment/', views.CommentCreateAPIView.as_view(), name='comment-create'),
]
views.py
from rest_framework.generics import ListAPIView, RetrieveAPIView, CreateAPIView
from blog.models import Post, Comment
from .serializers import CommentSerializer, PostSerializer
class PostListAPIView(ListAPIView):
queryset = Post.objects.all()
serializer_class = PostSerializer
class PostRetrieveAPIView(RetrieveAPIView):
queryset = Post.objects.all()
serializer_class = PostSerializer
class CommentCreateAPIView(CreateAPIView):
queryset = Comment.objects.all()
serializer_class = CommentSerializer
PostListAPIView
와 PostRetrieveAPIView
는 로직이 다른데 queryset, serializer_class 코드는 동일함 ➡ 서로 다른 클래스(ListAPIView, RetrieveAPIView)를 상속받기 때문에 실제 동작도 다르게 동작함!class PostListAPIView(ListAPIView):
queryset = Post.objects.all()
serializer_class = PostSerializer
queryset = Post.objects.all()
: DB에서 데이터를 가져옴serializer_class = PostSerializer
: serialize 수행2번 로직(serialize)
이 다름!many=True
)many=False
)many=False
를 인자로 주면 instance 하나만을 serialize 하라는 의미가 됨!ListAPIView (get -> list)
many=True
옵션을 주고 있음RetrieveAPIView (get -> retrieve)
many
인자는 따로 주지 않고 있음 -> default : many=False
https://www.inflearn.com/course/%EC%9E%A5%EA%B3%A0-drf/dashboard