1

视图集

简介

  • REST框架包括一个用于处理ViewSets的抽象,它允许开发人员集中精力对API的状态和交互进行建模,并根据常规约定自动处理URL构造。
  • ViewSet类与View类几乎相同,不同之处在于它们提供诸如read或update之类的操作,而不是get或put等方法处理程序
  • ViewSet类只绑定到一组方法处理程序,当它被实例化成一组视图的时候,通常通过使用一个Router类来处理自己定义URL conf的复杂性。

路由器

因为我们使用的是ViewSet类而不是View类,我们实际上不需要自己设计URL。将资源连接到视图和url的约定可以使用Router类自动处理。我们需要做的就是使用路由器注册相应的视图集,然后让它执行其余操作。

视图vs视图集之间的权衡

  • 使用视图集可以是一个非常有用的抽象。它有助于确保URL约定在你的API中保持一致,最大限度地减少编写所需的代码量,让你能够专注于API提供的交互和表示,而不是URLconf的细节。
  • 这并不意味着采用视图集总是正确的方法。在使用基于类的视图而不是基于函数的视图时,有一个类似的权衡要考虑。使用视图集不像单独构建视图那样明确。

不同的版本演练

版本一

  • idc/urls.py
from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'idcs/$',views.idc_list),
    url(r'idcs/(?P<pk>[0-9]+)/$',views.idc_detail),
]
  • models.py
from django.db import models
# Create your models here.
class Idc(models.Model):
    name = models.CharField("IDC名称",max_length=128,blank=False,null=True)
    address = models.CharField("IDC地址",max_length=200,default="")
    phone = models.CharField("IDC联系电话",max_length=20,null=True)
    email = models.EmailField("IDC邮箱")

    def __str__(self):
        return self.name

    class Meta:
        db_table = "idc"
  • serializers.py
from rest_framework import serializers
from .models import Idc

class IdcSerializer(serializers.Serializer):
    id = serializers.IntegerField(read_only=True)
    name = serializers.CharField(required=False)
    address = serializers.CharField(required=False)
    phone = serializers.CharField(required=False)
    email = serializers.EmailField(required=False)

    def update(self, instance, validated_data):
        instance.name = validated_data.get("name",instance.name)
        instance.address = validated_data.get("address",instance.address)
        instance.phone = validated_data.get("phone",instance.phone)
        instance.email = validated_data.get("email",instance.email)
        instance.save()
        return instance

    def create(self, validated_data):
        return Idc.objects.create(**validated_data)
  • views.py
from .models import Idc
from .serializers import IdcSerializer
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser
from django.http import HttpResponse,JsonResponse

class JSONResponse(HttpResponse):
    def __init__(self,data,**kwargs):
        kwargs.setdefault('content_type', 'application/json')
        content = JSONRenderer().render(data)
        super(JSONResponse,self).__init__(content=content,**kwargs)

def idc_list(request,*args,**kwargs):
    if request.method == "GET":
        queryset = Idc.objects.all()
        serializer = IdcSerializer(queryset,many=True)
        return JSONResponse(serializer.data)
        # content = JSONRenderer().render(serializer.data)
        # return HttpResponse(content,content_type="application/json")

    elif request.method == "POST":
        content = JSONParser().parse(request)
        serializer = IdcSerializer(data=content)
        if serializer.is_valid():
            serializer.save()
            content = JSONRenderer().render(serializer.data)
            return HttpResponse(content, content_type="application/json")

def idc_detail(request,pk,*args,**kwargs):
    try:
        idc = Idc.objects.get(pk=pk)
    except Idc.DoesNotExist:
        return HttpResponse(status=404)

    if request.method == "GET":
        serializer = IdcSerializer(idc)
        return JSONResponse(serializer.data)

    elif request.method == "POST":
        content = JSONParser().parse(request)
        serializer = IdcSerializer(data=content)
        if serializer.is_valid():
            serializer.save()
            return JSONResponse(serializer.data)

    elif request.method == "PUT":
        content = JSONParser().parse(request)
        serializer = IdcSerializer(idc,data=content)    #这里要注意传入instance(idc)否则就是创建操作
        if serializer.is_valid():
            serializer.save()
            return JSONResponse(serializer.data)

    elif request.method == "DELETE":
        idc.delete()
        return HttpResponse(status=204)

版本二

基于rest_framework下的:Response方法,status定义和api_view装饰器

  • idc/urls.py
## 版本二
from rest_framework.urlpatterns import format_suffix_patterns
urlpatterns = [
    url(r'^$',views.api_root),
    url(r'idcs/$',views.idc_list_v2,name="idc-list"),
    url(r'idcs/(?P<pk>[0-9]+)/$',views.idc_detail_v2,name='idc-detail'),
]
urlpatterns=format_suffix_patterns(urlpatterns)
  • idc/views.py
### 版本二
from rest_framework.response import Response
from rest_framework import status
from rest_framework.decorators import api_view

@api_view(['GET','POST'])
def idc_list_v2(request,*args,**kwargs):
    if request.method == "GET":
        queryset = Idc.objects.all()
        serializer = IdcSerializer(queryset,many=True)
        return Response(serializer.data)

    elif request.method == "POST":
        content = JSONParser().parse(request)
        serializer = IdcSerializer(data=content)
        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)

@api_view(['GET','POST','PUT','DELETE'])
def idc_detail_v2(request,pk,*args,**kwargs):
    try:
        idc = Idc.objects.get(pk=pk)
    except Idc.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == "GET":
        serializer = IdcSerializer(idc)
        return Response(serializer.data)

    elif request.method == "POST":
        content = JSONParser().parse(request)
        serializer = IdcSerializer(data=content)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)

    elif request.method == "PUT":
        content = JSONParser().parse(request)
        serializer = IdcSerializer(idc,data=content)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)

    elif request.method == "DELETE":
        idc.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)

from rest_framework.reverse import reverse
@api_view(['GET'])
def api_root(request,format=None,*args,**kwargs):
    return Response({
        "idcs":reverse("idc-list",request=request,format=format)
    })
  • 测试:

浏览器打开:http://192.168.33.10:8080/idcs/ 即可

版本三

基于类视图的版本,要注意接受提交数据用的是request.data

  • idc/urls.py
###版本三
from rest_framework.urlpatterns import format_suffix_patterns
urlpatterns = [
    url(r'^$',views.api_root),
    url(r'idcs/$',views.IdcListView.as_view,name="idc-list"),
    url(r'idcs/(?P<pk>[0-9]+)/$',views.IdcDetailView.as_view,name='idc-detail'),
]
urlpatterns=format_suffix_patterns(urlpatterns)
  • idc/views.py
### 版本三
from rest_framework.views import APIView
from django.http import Http404

class IdcListView(APIView):
    def get(self,request,format=None):
        queryset = Idc.objects.all()
        serializer = IdcSerializer(queryset, many=True)
        return Response(serializer.data)

    def post(self,request,format=None):
        #content = JSONParser().parse(request)
        serializer = IdcSerializer(data=request.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 IdcDetailView(APIView):
    def get_obj(self,pk):
        try:
            return Idc.objects.get(pk=pk)
        except Idc.DoesNotExist:
            raise Http404

    def get(self,request,pk,format=None):
        idc = self.get_obj(pk)
        serializer = IdcSerializer(idc)
        return Response(serializer.data)

    def put(self,request,pk,format=None):
        idc = self.get_obj(pk)
        serializer = IdcSerializer(idc,data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.data,status=status.HTTP_400_BAD_REQUEST)

    def delete(self,request,pk,format=None):
        idc = self.get_obj(pk)
        idc.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)

版本四

  • idc/urls.py
###版本四
from rest_framework.urlpatterns import format_suffix_patterns
urlpatterns = [
    url(r'^$',views.api_root),
    url(r'idcs/$',views.IdcList_V4.as_view(),name="idc-list"),
    url(r'idcs/(?P<pk>[0-9]+)/$',views.IdcDetail_V4.as_view(),name='idc-detail'),
]
urlpatterns=format_suffix_patterns(urlpatterns)
  • views.py
###版本四
from rest_framework import mixins,generics

class IdcList_V4(generics.GenericAPIView,
                 mixins.ListModelMixin,
                 mixins.CreateModelMixin):
    queryset = Idc.objects.all()
    serializer_class = IdcSerializer

    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 IdcDetail_V4(generics.GenericAPIView,
                   mixins.RetrieveModelMixin,
                   mixins.UpdateModelMixin,
                   mixins.DestroyModelMixin):
    queryset = Idc.objects.all()
    serializer_class = IdcSerializer
    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)

版本五

  • urls.py
###版本五
from rest_framework.urlpatterns import format_suffix_patterns
urlpatterns = [
    url(r'^$',views.api_root),
    url(r'idcs/$',views.IdcList_V5.as_view(),name="idc-list"),
    url(r'idcs/(?P<pk>[0-9]+)/$',views.IdcDetail_V5.as_view(),name='idc-detail'),
]
urlpatterns=format_suffix_patterns(urlpatterns)
  • views.py
### 版本五
class IdcList_V5(generics.ListCreateAPIView):
    queryset = Idc.objects.all()
    serializer_class = IdcSerializer

class IdcDetail_V5(generics.RetrieveUpdateDestroyAPIView):
    queryset = Idc.objects.all()
    serializer_class = IdcSerializer

版本六

  • urls.py
## 版本六
idc_list = views.IdcViewSet.as_view({
    "get":"list",
    "post":"create"
})
idc_detail = views.IdcViewSet.as_view({
    "get":"retrieve",
    "put":"update",
    "delete":"destroy"
})
urlpatterns = [
    url(r'^$',views.api_root),
    url(r'idcs/$',idc_list,name="idc-list"),
    url(r'idcs/(?P<pk>[0-9]+)/$',idc_detail,name='idc-detail'),
]
urlpatterns=format_suffix_patterns(urlpatterns)
  • views.py
####版本六
from rest_framework import viewsets
class IdcViewSet(viewsets.GenericViewSet,
                 mixins.RetrieveModelMixin,
                 mixins.UpdateModelMixin,
                 mixins.DestroyModelMixin,
                 mixins.ListModelMixin,
                 mixins.CreateModelMixin):
    queryset = Idc.objects.all()
    serializer_class = IdcSerializer

版本七

  • urls.py
###版本七
from rest_framework.routers import DefaultRouter
router = DefaultRouter()
router.register("idcs",views.IdcViewSet_V7) #自动适配,字啊需要加的话就直接加
urlpatterns = [
    url(r'^',include(router.urls))
]
  • views.py
### 版本七
class IdcViewSet_V7(viewsets.ModelViewSet):
    queryset = Idc.objects.all()
    serializer_class = IdcSerializer

wanghui
34 声望9 粉丝