django rest_framework入门四-类视图APIView
2021-04-08 09:27
标签:通用 使用 tail self 入门 面向对象 href 方法 return 上节,我们使用函数视图,用了@api_view装饰器来修饰,这一节,我们介绍类视图APIView,显然,类视图更符合面向对象的原则。 1.使用类视图APIView重写API 2.编辑urls.py APIView视图类,调用as_view()方法 3.使用Mixins SnippetList继承了mixins的ListModelMixin和CreateModelMixin类, 4.使用通用类视图generics 使用generics的ListCreateAPIView和RetrieveUpdateDestroyAPIView替代mixins类 5.使用viewsets进一步简化 django rest_framework入门四-类视图APIView 标签:通用 使用 tail self 入门 面向对象 href 方法 return 原文地址:https://www.cnblogs.com/shijingjing07/p/9089215.html
类视图APIView,取代@api_view装饰器,代码如下:from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView
from snippets.models import Snippet
from snippets.serializers import SnippetSerializer
class SnippetList(APIView):
def get(self, request):
snippets = Snippet.objects.all()
serializer = SnippetSerializer(snippets, many=True)
return Response(serializer.data)
def post(self, request):
data = request.data
serializer = SnippetSerializer(data=data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class SnippetDetail(APIView):
def get_object(self, pk):
try:
return Snippet.objects.get(pk=pk)
except Exception as e:
pass
def get(self, request, pk):
snippet = self.get_object(pk)
serializer = SnippetSerializer(snippet)
return Response(serializer.data)
def put(self, request, pk):
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):
snippet = self.get_object(pk)
snippet.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
from django.conf.urls import url, include
from django.contrib import admin
from snippets import views
urlpatterns = [
url(r'^snippets/$', views.SnippetList.as_view()),
url(r'^snippets/(?P
Mixins封装了增删改查的操作,因此可以使用Mixins简化代码,如下:from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework import mixins, generics
from snippets.models import Snippet
from snippets.serializers import SnippetSerializer
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)
赋值queryset,serializer_class属性,
然后定义了get,post方法,分别调用mixins的list,create函数。
SnippetDetail定义类似。
rest_framework定义了可能的增删改查组合,优化了多重继承的繁杂from rest_framework import generics
from snippets.models import Snippet
from snippets.serializers import SnippetSerializer
class SnippetList(generics.ListCreateAPIView):
queryset = Snippet.objects.all()
serializer_class = SnippetSerializer
class SnippetDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = Snippet.objects.all()
serializer_class = SnippetSerializer
同时,去掉了get,post方法
viewsets包含了对一个model的增删改查全部操作,代码如下:from rest_framework import viewsets
from snippets.models import Snippet
from snippets.serializers import SnippetSerializer
class SnippetViewSet(viewsets.ModelViewSet):
queryset = Snippet.objects.all()
serializer_class = SnippetSerializer
文章标题:django rest_framework入门四-类视图APIView
文章链接:http://soscw.com/index.php/essay/72809.html