視圖類用法大總結

視圖家族

from rest_framework import views, generics, mixins, viewsets
views:視圖類
mixins:視圖工具類
generics:工具視圖類
viewsets:視圖集
#倆大視圖類:
from rest_framework.views import APIView
from rest_framework.generics import GenericAPIView
# APIView,GenericAPIView
# GenericAPIView:是全部工具視圖的父類
# 六大視圖工具類:
from rest_framework.mixins import RetrieveModelMixin,ListModelMixin,CreateModelMixin,UpdateModelMixin,DestroyModelMixin
# RetrieveModelMixin,單查詢
# ListModelMixin,羣查詢
# CreateModelMixin,增長
# UpdateModelMixin,更新,在這裏面有兩種方法,所以是六大視圖工具類
# DestroyModelMixin,刪除
# 九大工具視圖類
from rest_framework.generics import CreateAPIView,DestroyAPIView,GenericAPIView,ListAPIView,ListCreateAPIView,RetrieveAPIView,RetrieveDestroyAPIView,RetrieveUpdateAPIView,RetrieveUpdateDestroyAPIView,UpdateAPIView
# CreateAPIView,單增長
# DestroyAPIView,刪除
# GenericAPIView,基類
# ListAPIView,羣查
# ListCreateAPIView,羣查,單增長
# RetrieveAPIView,單查
# RetrieveDestroyAPIView,
# RetrieveUpdateAPIView,
# RetrieveUpdateDestroyAPIView,
# UpdateAPIView
#兩大視圖集基類
from rest_framework.viewsets import ViewSet,GenericViewSet 
# ViewSet,
# GenericViewSet

數據準備

from django.db import models

# Create your models here.

class Student(models.Model):
    name = models.CharField(max_length=64)

APIView

from rest_framework.views import APIView

APIView繼承Django的View
1) View:將請求方式與視圖類的同名方法創建映射,完成請求響應
2) APView:
主要繼承了View的功能;
重寫as_view禁用csrf認證;
重寫dispatch:請求、響應、渲染、異常、解析、三大認證
多了一堆類屬性,能夠完成視圖類的局部配置django

from . import models, serializers
from rest_framework.response import Response
class StudentAPIView(APIView):
    def get(self, request, *args, **kwargs):
        stu_query = models.Student.objects.all()
        stu_ser = serializers.StudentModelSerializer(stu_query, many=True)
        return Response(stu_ser.data)
url(r'^v1/students/$', views.StudentAPIView.as_view()),
    url(r'^v1/students/(?P<pk>\d+)/$', views.StudentAPIView.as_view()),

GenericAPIView

from rest_framework.generics import GenericAPIView
class GenericAPIView(views.APIView):#繼承了views.APIView的全部功能
# GenericAPIView提供了三個方法:get_object()、get_queryset()、get_serializer()
class StudentGenericAPIView(GenericAPIView):
    queryset = models.Student.objects.all()
    serializer_class = serializers.StudentModelSerializer
    def get(self, request, *args, **kwargs):
        # stu_query = models.Student.objects.all()
        stu_query = self.get_queryset()
        # stu_ser = serializers.StudentModelSerializer(stu_query, many=True)
        stu_ser = self.get_serializer(stu_query, many=True)
        return Response(stu_ser.data)
url(r'^v2/students/$', views.StudentGenericAPIView.as_view()),
    url(r'^v2/students/(?P<pk>\d+)/$', views.StudentGenericAPIView.as_view()),

視圖工具類

from rest_framework import mixins
class StudentMixinGenericAPIView(mixins.ListModelMixin, mixins.CreateModelMixin, GenericAPIView):
    queryset = models.Student.objects.all()
    serializer_class = serializers.StudentModelSerializer
    def get(self, request, *args, **kwargs):
        return self.list(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        return self.create(request, *args, **kwargs)
url(r'^v3/students/$', views.StudentMixinGenericAPIView.as_view()),
    url(r'^v3/students/(?P<pk>\d+)/$', views.StudentMixinGenericAPIView.as_view()),

工具視圖類(重點)

主要是查詢是最重要的api

單查詢和羣查詢不能同時使用iview

# 工具視圖類
from rest_framework.generics import CreateAPIView, RetrieveAPIView, ListAPIView, UpdateAPIView, DestroyAPIView
class StudentMixinAPIView(CreateAPIView, RetrieveAPIView, ListAPIView, UpdateAPIView, DestroyAPIView):
    queryset = models.Student.objects.all()
    serializer_class = serializers.StudentModelSerializer
    # url中單查,不必定必須提供主鍵,提供一切惟一鍵的字段名都可
    lookup_url_kwarg = 'id'

    # 有刪除需求的接口繼承DestroyAPIView,重寫destroy完成字段刪除
    def destroy(self, request, *args, **kwargs):
        pass
url(r'^v4/students/$', views.StudentMixinAPIView.as_view()),
    url(r'^v4/students/(?P<id>\d+)/$', views.StudentMixinAPIView.as_view()),

    # 將全部 請求方式 與 響應方法名 的映射關係交給用戶本身配置

視圖集

能夠解決單查和羣查詢工具

from rest_framework.viewsets import ModelViewSet
class StudentModelViewSet(ModelViewSet):
    queryset = models.Student.objects.all()
    serializer_class = serializers.StudentModelSerializer

    def my_post(self, request, *args, **kwargs):
        return Response('my post ok')
url(r'^v5/students/$', views.StudentModelViewSet.as_view({
        'get': 'list',
        'post': 'my_post'
    })),
    url(r'^v5/students/(?P<pk>\d+)/$', views.StudentModelViewSet.as_view({
        'get': 'retrieve',
        'put': 'update',
        'patch': 'partial_update',
        'delete': 'destroy'
    })),
相關文章
相關標籤/搜索