DRF - ViewSet
ViewSet
get
、post
などの方法ハンドルを提供しないが、list
、create
などの動作を提供する.Code
修正テストは、前のDRFのAuth部分コードに続いて行われる.
ViewSet
profiles.api.views.py
変更前from rest_framework import generics
from rest_framework.permissions import IsAuthenticated
from profiles.models import Profile
from profiles.api.serializers import ProfileSerializer
class ProfileList(generics.ListAPIView):
queryset = Profile.objects.all()
serializer_class = ProfileSerializer
permission_classes = [IsAuthenticated]
変更後from rest_framework.viewsets import ReadOnlyModelViewSet
class ProfileViewSet(ReadOnlyModelViewSet):
queryset = Profile.objects.all()
serializer_class = ProfileSerializer
permission_classes = [IsAuthenticated]
profiles.api.urls.py
変更前from django.urls import path
from profiles.api.views import ProfileList
urlpatterns = [
path("profiles/", ProfileList.as_view(), name="profile-list")
]
変更後
from rest_framework import generics
from rest_framework.permissions import IsAuthenticated
from profiles.models import Profile
from profiles.api.serializers import ProfileSerializer
class ProfileList(generics.ListAPIView):
queryset = Profile.objects.all()
serializer_class = ProfileSerializer
permission_classes = [IsAuthenticated]
from rest_framework.viewsets import ReadOnlyModelViewSet
class ProfileViewSet(ReadOnlyModelViewSet):
queryset = Profile.objects.all()
serializer_class = ProfileSerializer
permission_classes = [IsAuthenticated]
from django.urls import path
from profiles.api.views import ProfileList
urlpatterns = [
path("profiles/", ProfileList.as_view(), name="profile-list")
]
from django.urls import path
from profiles.api.views import ProfileViewSet
profile_list = ProfileViewSet.as_view({"get": "list"})
profile_detail = ProfileViewSet.as_view({"get": "retrieve"})
urlpatterns = [
path("profiles/", profile_list, name="profile-list"),
path("profiles/<int:pk>/", profile_detail, name="profile-detail")
]
ViewSet + Router
profiles.api.urls.py
routerを使用すると、パス指定はDRFによって自動的に実行され、通常viewsetのurlはrouterに設定されます.
from django.urls import include, path
from rest_framework.routers import DefaultRouter
from profiles.api.views import ProfileViewSet
# router는 자동으로 link들을 생성해낸다.
router = DefaultRouter()
router.register(r"profiles", ProfileViewSet)
urlpatterns = [
path("", include(router.urls))
]
テスト結果router.register() method
router.register(r"status", ProfileStatusViewSet, basename="status")
必須パラメータViewSet+Update機能の追加
profiles.api.views.py
from rest_framework import viewsets
from rest_framework import mixins
from profiles.api.permissions import IsOwnProfileOrReadOnly
class ProfileViewSet(mixins.UpdateModelMixin,
mixins.ListModelMixin,
mixins.RetrieveModelMixin,
viewsets.GenericViewSet):
queryset = Profile.objects.all()
serializer_class = ProfileSerializer
permission_classes = [IsAuthenticated, IsOwnProfileOrReadOnly]
profiles.api.permissions.pyfrom rest_framework import permissions
class IsOwnProfileOrReadOnly(permissions.BasePermission):
def has_object_permission(self, request, view, obj):
if request.method in permissions.SAFE_METHODS:
return True
return obj.user == request.user
結果ViewSet+update&delete機能の追加
profiles.api.views.py
from rest_framework.viewsets import ModelViewSet
from profiles.models import ProfileStatus
from profiles.api.serializers import ProfileStatusSerializer
from profiles.api.permissions import IsOwnerOrReadOnly
class ProfileStatusViewSet(ModelViewSet):
queryset = ProfileStatus.objects.all()
serializer_class = ProfileStatusSerializer
permission_classes = [IsAuthenticated, IsOwnerOrReadOnly]
def perform_create(self, serializer):
user_profile = self.request.user.profile
serializer.save(user_profile=user_profile)
profiles.api.permissions.pyclass IsOwnerOrReadOnly(permissions.BasePermission):
def has_object_permission(self, request, view, obj):
if request.method in permissions.SAFE_METHODS:
return True
return obj.user_profile == request.user.profile
profiles.api.urls.pyfrom profiles.api.views import ProfileViewSet, ProfileStatusViewSet
# 추가
router.register(r"status", ProfileStatusViewSet)
結果AvatarUpdateViewの追加
profiles.api.views.py
from profiles.api.serializers import ProfileAvatarSerializer
class AvatarUpdateView(generics.UpdateAPIView):
serializer_class = ProfileAvatarSerializer
permission_classes = [IsAuthenticated]
def get_object(self):
profile_object = self.request.user.profile
return profile_object
profiles.api.urls.pyfrom profiles.api.views import ..., AvatarUpdateView
urlpatterns = [
...,
path("avatar/", AvatarUpdateView.as_view(), name="avatar-update")
]
結果良いウェブサイト
Reference
この問題について(DRF - ViewSet), 我々は、より多くの情報をここで見つけました https://velog.io/@nameunzz/DRF-ViewSetテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol