源码剖析Django REST framework的请求生命周期

学习Django的时候知道,在Django请求的生命周期中,请求经过WSGI和中间件到达路由,不管是FBV还是CBV都会先执行View视图函数中的dispatch方法

REST framework是基于Django的API框架,REST framework采用的是CBV的请求模式.

所以在一个项目中,使用了REST framework的时候,

请求到达REST framework后,也先执行REST framework中的dispatch方法

先来看看dispatch方法的源码

def dispatch(self, request, *args, **kwargs): self.args = args # 函数传递过来的参数 self.kwargs = kwargs # 函数传递过来的参数 # 封装request request = self.initialize_request(request, *args, **kwargs) self.request = request self.headers = self.default_response_headers # deprecate? try: self.initial(request, *args, **kwargs) 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) except Exception as exc: response = self.handle_exception(exc) self.response = self.finalize_response(request, response, *args, **kwargs) return self.response

查看initialize_request方法,可以知道这个方法接收客户端的request请求,再重新封装成新的request

def initialize_request(self, request, *args, **kwargs): parser_context = self.get_parser_context(request) return Request( request, parsers=self.get_parsers(), authenticators=self.get_authenticators(), negotiator=self.get_content_negotiator(), parser_context=parser_context )

再查看Request方法的源码

可以知道这个Request类是rest framework中定义的一个类

class Request(object): def __init__(self, request, parsers=None, authenticators=None, negotiator=None, parser_context=None): self._request = request self.parsers = parsers or () self.authenticators = authenticators or () self.negotiator = negotiator or self._default_negotiator() self.parser_context = parser_context self._data = Empty self._files = Empty self._full_data = Empty self._content_type = Empty self._stream = Empty if self.parser_context is None: self.parser_context = {} self.parser_context['request'] = self self.parser_context['encoding'] = request.encoding or settings.DEFAULT_CHARSET force_user = getattr(request, '_force_auth_user', None) force_token = getattr(request, '_force_auth_token', None) if force_user is not None or force_token is not None: forced_auth = ForcedAuthentication(force_user, force_token) self.authenticators = (forced_auth,)

先不看这个Request到底执行了什么操作

但是已经知道经过Request处理过的request已经不再是客户端发送过来的那个request了

在initialize_request方法中,有一个方法处理过request,来看看get_parser_context方法的源码

def get_parser_context(self, http_request): return { 'view': self, 'args': getattr(self, 'args', ()), 'kwargs': getattr(self, 'kwargs', {}) }

在这里,view的值是self,代指的是UsersView这个对象,所以get_parser_context方法把UsersView这个类封装进来然后返回

所以get_parser_context方法最后返回的当前对象以及当前对象所传的参数

经过initialize_request函数处理之后的request,现在就变成了

Request( request, parsers=self.get_parsers(), authenticators=self.get_authenticators(), negotiator=self.get_content_negotiator(), parser_context=parser_context )

现在再来看看Request的其他参数代指的是什么

get_parsers 根据字面意思,是解析get请求的意思 get_authenticators 认证相关 get_content_negotiator 选择相关 parser_context 封闭self和self的参数 def get_parsers(self): return [parser() for parser in self.parser_classes] def get_authenticators(self): return [auth() for auth in self.authentication_classes] def get_permissions(self): return [permission() for permission in self.permission_classes] def get_throttles(self): return [throttle() for throttle in self.throttle_classes] def get_content_negotiator(self): if not getattr(self, '_negotiator', None): self._negotiator = self.content_negotiation_class() return self._negotiator

再来看看UsersView这个类中的get方法和post方法

def get(self,request,*args,**kwargs): pass def post(self,request,*args,**kwargs): pass

可以看到get方法的参数中有一个request,通过前面可以知道这个request已经不是最开始时到达服务端的request了

这个request方法中已经被REST framework封装了解析,认证和选择等相关的方法

def dispatch(self, request, *args, **kwargs): self.args = args self.kwargs = kwargs request = self.initialize_request(request, *args, **kwargs) self.request = request self.headers = self.default_response_headers # deprecate? try: self.initial(request, *args, **kwargs) 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) except Exception as exc: response = self.handle_exception(exc) self.response = self.finalize_response(request, response, *args, **kwargs) return self.response

default_response_headers这个方法从它的注释可以看出已经被丢弃了.

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpzfds.html