Third, to achieve several views source analysis: django native View, rest_framework.views.APIView, rest_framework.generics.GenericAPIView, ViewSetMixin

A, django native View:

  1, start the program, a preloaded url, execution as_view (), the function returns view

path('book/', views.BookView.as_view()),

 

  2, user access methods to perform view, into dispatch:

 

 

   3, dispatch return value self.get of execution, view view call dispatch, view class BookView call, so the return value is the return value of the dispatch of view, the return value is the view dispatch return value that is returned to the value of the page .

 

II: Use of rest_framework.views.APIView request process:

re_path('^book/(\d+)', views.BookDetailView.as_view()),

  1, APIView succession django native View, as_view method in the superclass APIView return as_view view of the return value

 

   2, user access methods to perform view, into dispatch: to achieve a dispatch method under APIView, so dispatch under APIView takes precedence:

 

   3, execution returns response dispatch, the return value is equal to the view response. view call dispatch of view, the view class BookView call, so the view of the return value is the response, that is, the return value dispatch, dispatch return value is the view of the return value, that value is returned to the page.

  

 

Three, rest_framework.generics.GenericAPIView: it is just a simple packaging at APIView, the original view of the redundant code implemented in several different classes packaged under

path('author/', views.AuthorView.as_view()),
class AuthorView(mixins.ListModelMixin, mixins.CreateModelMixin, generics.GenericAPIView):
    queryset = models.Author.objects.all()
    serializer_class = serializers.AuthorSerializers

    def get(self, request, *args, **kwargs):
        return self.list(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        return self.create(request, *args, **kwargs)

  1, in order to view the program is loaded to the father as_view method, mixins.ListModelMixin, did not find the next mixins.CreateModelMixin, under generics.GenericAPIView did not, but it inherits APIView, there are at APIView, execute it, and return to view.

 

   2, the implementation of user access to view, view of the return value is the return value of dispatch, execution time priority dispatch performed under APIView, dispatch return value is the return value of the view

 

   3, back view, the return value of the list method return values ​​for a view, list view class does not implement the method, in order to find the parent

 

   4, mixins.ListModelMixin method has a list, execute it, it returns a sequence of data, the data is returned by the view, and then return to the dispatch, and then view. But this process is how to achieve it, look at the method get_queryset

 

   5, get_queryset: This method is found in generics.GenericAPIView

 

  

  6, list tone get_queryset, adjusting the view list, so self.queryset is the view class queryset

 

   7, list after we give to the method in the view object serialization return queryset

 

Four, viewsets.ModelViewSet in ViewSetMixin

 path('book/', views.BookView.as_view({'get': 'list', 'post': 'create'})),

  1, the program execution as_view of acquaintance, in which ViewSetMixin, is not native django as_view under View, at this time it receives an action parameter, i.e. to the dictionary in the url

  2, user access, execution View, this time "get", "post" and the like string equals self.list self.creater

 

   3, step by step investigation dispatch, did not realize at ViewSetMixin, GenericAPIView did not realize, GenericAPIView inheritance APIView, APIView realized

 

 

 

   4, executed under the dispatch APIView, execution returns to self.list

 

   5, looking for self.list, find the list method in class it is ListModelMixin

 

 

 to sum up:

  When the view class inherits django native views.View: as_view () Returns the view, its return value is equal to dispatch the return value, the return value is equal to the dispatch view class get, post or the like is performed

  When the view class inherits APIView under rest_framework, as_view () is still performed under native as_view View class, but APIView covering dispatch method, so the return value is the view of the dispatch APIView performed. It reconstructs a new request

Because when the above two ways to achieve the view, too much redundant code, as mixins class to streamline the code:

  Inheritance ListCreateAPIView and RetrieveUpdateDestroyAPIView class, only need to provide queryset objects can be serialized class. Dispatch and execution APIView as_view consistent and only the last step in view to get, post or the like is encapsulated in mixins class.

  viewsets.ModelViewSet class can be achieved by way of url requests all can be realized only with a CBV. Mainly as_view execution is no longer a native as_view under the View class, but as_view under ViewSetMixin. It returns the view when the user accesses executed, perform the method of the dictionary corresponding to the url after control treatment is performed to dispatch, he can find the corresponding execution classes and methods in the class mixins methods in view, e.g. self.list, self .create

 

Guess you like

Origin www.cnblogs.com/aizhinong/p/12543799.html