Rest framework-APIView简单源码分析

REST-APIView源码分析

前言:APIView基于View 看这部分内容一定要懂django—CBV里的内容

在django—CBV源码分析中,我们是分析的from django.views import View下的执行流程
这篇博客我们就来了解下APIView是如何执行的,跟django.views模块下的view有何关联?
我们依然从urls.py配置入手分析

from django.conf.urls import url
from django.contrib import admin
from app01 import views

urlpatterns = [
    url(r'^publishes/', views.PublishView.as_view()),
]

views.py

from rest_framework.views import  APIView

class PublishView(APIView):

    def get(self,request):
        publish_list=Publish.objects.all()
        ret=serialize("json",publish_list)

        return HttpResponse(ret)

    def post(self,request):
        pass

1、首先我们还是来确认urls.pyas_view是谁执行的?
首先我们去views.PublishView中找,发现找不到,所以我们接着再去PublishView的父类APIView中去找,找到了所以执行调用APIView.as_view(),内容如下:

class APIView(View):

    # 多余的代码暂且删掉了
    @classmethod
    def as_view(cls, **initkwargs):
        if isinstance(getattr(cls, 'queryset', None), models.query.QuerySet):
            def force_evaluation():
                raise RuntimeError(
                    'Do not evaluate the `.queryset` attribute directly, '
                    'as the result will be cached and reused between requests. '
                    'Use `.all()` or call `.get_queryset()` instead.'
                )
            cls.queryset._fetch_all = force_evaluation
        
        # 1. super调用父类的as_view的执行结果赋值给view这个变量
        view = super(APIView, cls).as_view(**initkwargs)
        view.cls = cls
        view.initkwargs = initkwargs

        # Note: session based authentication is explicitly CSRF validated,
        # all other authentication is CSRF exempt.
        
        # csrf_exempt 不在执行调用csrf_token那个中间件
        # 中间件是针对全局的 
        # 2. 这里返回的view就是父类View.view
        return csrf_exempt(view)

2、大家是不是以为这样就结束了?NO!NO!NO!
如果是as_view是View的as_view,dispatch是View的dispatch,那rest-framework不就成废钞了么?
as_view的执行结果是dispatch的执行结果,那么dispatch还是View的dispatch么?

  1. 我们先看下views.PublishView里边有没有dispatch,发现没有
  2. 我们在views.PublishView的父类APIView,发现有
  3. 所以dispatch是APIView.dispatch非View.dispatch
  4. APIView.dispatch 里边有一堆组件,这里不说

中间的这点代码跟之前的View.dispatch一样:
还是做请求分发,在请求之外又做了一堆事情

    if request.method.lower() in self.http_method_names:
        handler = getattr(self, request.method.lower(),
                          self.http_method_not_allowed)
    else:
        handler = self.http_method_not_allowed

    response = handler(request, *args, **kwargs)

总结:

url 转变过程

  1. url(r'^publishes/', views.PublishView.as_view()),
  2. url(r'^publishes/', APIView.as_view()),
  3. url(r'^publishes/', View.as_view()),

View.as_view()我们在django—CBV讲解过了
所以最后调用还是,只是APIView.as_view在里边加了一些他自己定义的一些东西,只是这里没讲到而已

APIView.as_view()---> View.as_view()---> APIView.dispatch()--->response--->handler()--> 自己定义请求方法函数的返回结果,否则就抛错405

你可能感兴趣的:(Rest framework-APIView简单源码分析)