django rest framework : method override
Description
- viewset の指定 method(create, updateなど) を上書きする
上書き
以下, 対応する method を上書きする
api/views.py
from rest_framework import viewsets
from rest_framework.response import Response
from api.models import User
from .serializers import UserSerializer
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all().select_related()
serializer_class = UserSerializer
def list(self, request):
# GET : /api/users/
def create(self, request):
# POST : /api/users/
def retrieve(self, request, pk=None):
# GET : /api/users/:id/
def update(self, request, pk=None):
# PUT : /api/users/:id/
def partial_update(self, request, pk=None):
# PATCH : /api/users/:id/
def destroy(self, request, pk=None):
# DELETE : /api/users/:id/
list 実装例
api/views.py
from rest_framework import viewsets
from rest_framework.response import Response
from api.models import User
from .serializers import UserSerializer
class UserViewSet(viewsets.ModelViewSet):
"""
list:
Return a list of all the existing users.
"""
queryset = User.objects.all().select_related()
serializer_class = UserSerializer
def list(self, request):
"""
list:
Return a list of all the existing users.
"""
# 全 user 情報を取得し, serialize
data = UserSerializer(User.objects.all(), many=True).data
# http response として返す
return Response(status=200, data=data)
参考
api/views.py
from rest_framework import viewsets
from rest_framework.response import Response
from api.models import User
from .serializers import UserSerializer
class UserViewSet(viewsets.ModelViewSet):
"""
list:
Return a list of all the existing users.
"""
queryset = User.objects.all().select_related()
serializer_class = UserSerializer
def list(self, request):
"""
list:
Return a list of all the existing users.
"""
# 全 user 情報を取得し, serialize
data = UserSerializer(User.objects.all(), many=True).data
# http response として返す
return Response(status=200, data=data)
Author And Source
この問題について(django rest framework : method override), 我々は、より多くの情報をここで見つけました https://qiita.com/Satoshi_Numasawa/items/10a9b9fb1f17f0704faf著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .