View of the assembly sequence of the family drf

First, the view of the family of classification

1. Import Category
from rest_framewok import views, generics, mixins, viewsets

views: the view class

View two categories: APIView, GenericAPIView

from rest_framework.views import APIView
from rest_framework.generics import GenericAPIView

mixins: View Tools

Six view tools: RetrieveModelMixin, ListModelMixin, CreateModelMixin, UpdateModelMixin, DestroyModelMixin

from rest_framework.mixins import RetrieveModelMixin, ListModelMixin, CreateModelMixin, UpdateModelMixin, DestroyModelMixin

generics: Tools view class

Nine tools view class: ...

from rest_framework import generics

viewsets: View Sets

Liangda viewset base class: ViewSet, GenericViewSet

from rest_framework import viewsets

2.APIVIiew features

It inherits Django's View

1) View: The same method on the request to a view of a mapping class, in response to a request to complete

​ 2)APIView:

View inherits all the features;

Rewrite as_view disabled csrf certification;

Rewrite dispatch: request, response, rendering, an exception, parsing, three certification

A bunch of multi-class attributes may be accomplished partial view class

Second, the usage and the difference between the two views view class view class

APIView:

from rest_framework.views import APIView
from rest_framework.response import Response
from . import models,serializers

# APIView:
class StudentAPIView(APIView):
    def get(self, request, *args, **kwargs):
        # 群查
        stu_query = models.Sudent.objects.all()
        stu_ser = serializers.StudentModelSerializer(stu_query,many=True)
        print(stu_ser)
        return Response(stu_ser.data)

GenericAPIView:

# GenericAPIView:
from rest_framework.generics import GenericAPIView

class StudentGenericAPIView(GenericAPIView):
    queryset = models.Sudent.objects.all()
    serializer_class = serializers.StudentModelSerializer
    def get(self, request, *args, **kwargs):
        # 群查
        # stu_query = models.Sudent.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)

the difference:

1.GenericAPIView inherited APIView, so it can be used for all functions APIView
provided inside three common methods 2.GenericAPIView:
that get_object (): get the target sequence of a single preparation for single check
get_queryset (): get comprising Queryset plurality of data objects, a check group
get_serializer (): get the serialized in serializer objects
3. the three common attributes:
QuerySet
serializer_class
lookup_url_kwarg

Third, the view of usage and tools Mixin introduction

Increase in single and group check, for example:

from rest_framework import mixins
class StudentMixinGenericAPIView(mixins.ListModelMixin, mixins.CreateModelMixin, GenericAPIView):
    queryset = models.Sudent.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)

Features:
1. Providing five and six tools Tools Method:
CreateModelMixin: Create () implemented by a single
ListModelMixin: list () group to achieve search
RetrieveModelMixin: retrieve () to check for single
UpdateModelMixin: update () and change the single-perform_update () implemented locally modified
DestroyModelMixin: destroy () single-deleted

2. As long as the method call tools, can realize the function of the method, the internal implementation principle is said to be the code we wrote before a layer of packaging, so you can call us directly

3. Because of mixins five tools in the class does not inherit any view class views, no as_view () method in the allocation url, that is not any CRUD, so when writing class inherits GenericAPIView view class

Fourth, the view class Mixin tool usage and introduction

# 工具视图类
from rest_framework.generics import CreateAPIView, RetrieveAPIView, ListAPIView,UpdateAPIView,DestroyAPIView
class StudentMixinAPIView(CreateAPIView,ListAPIView,RetrieveAPIView,UpdateAPIView,DestroyAPIView):
    queryset = models.Sudent.objects.all()
    serializer_class = serializers.StudentModelSerializer
    # url中单查,不一定必须提供主键,提供一切唯一键的字段名均可
    lookup_url_kwarg = 'id'

    # 有删除需求的接口继承DestroyAPIView,重写destroy完成字段删除
    def destroy(self, request, *args, **kwargs):
        pass
 

analysis:

lookup_url_kwarg: url single check, do not necessarily have to provide a primary key, unique key field name to provide all the can, url configuration must be changed to id pk

Advantages:

CreateAPIView, ListAPIView, RetrieveAPIView, UpdateAPIView, DestroyAPIView five tools integrate mixins and GenericAPIView inside the class. They will make another package will get, post ... and other methods package together, we have the direct successor to the method of the class.
Disadvantages:

Shan Zha investigation can not coexist with the group, determines the order of succession according to a single check or check group, set of views described below can be done coexist.

V. usage with the introduction of view set

# 视图集
from rest_framework.viewsets import ModelViewSet
class StudentModelViewSet(ModelViewSet):
    queryset = models.Sudent.objects.all()
    serializer_class = serializers.StudentModelSerializer

    def mypost(self, request, *args, **kwargs):
        return Response('my post ok')

analysis:

See reason from the source code by using a set of views that can be achieved with a single check to check groups coexist:

ModelViewSet inheritance beyond the five tools also inherited GenericViewSet

GenericViewSet inherited ViewSet then inherited the ViewSetMixin

In ViewSetMixin class which overwrites the as_view () method, according to the inheritance relationship, if the matching on the route, as_view () method of ViewSetMixin go. In its as_view () method which, by means of which method to pass parameters to as_view (), the corresponding tools Method:

Its principle is to pass through the dictionary, reflected by the dictionary data, a method of obtaining the request wants to perform.

In the url routing configuration, so that we can check the difference between single and group checked:

We can also override the corresponding request method to execute their own. To achieve special needs.

Note: the code can be known from the above: In addition to inheriting the class APIView view, the other view class should be set in the class two properties:

queryset = models.Student.objects.all()  # 代表跟哪张表建立关系
serializer_class = serializers.StudentModelSerializer  # 指明用的是哪个序列化器

Guess you like

Origin www.cnblogs.com/guapitomjoy/p/11924775.html