Django rest framework 第一章

面向对象的补充:
面向对象的继承机制,子类中调用的方法如果子类里面没有,则向父类里面找,还没有则继续向上找
所有的self.function都是从自己的类中开始查找的



cbv流程:url-->views-->dispatch

drf源码流程
当请求进来之后:
1.根据上面的流程我们先执行url-->views-->dispatch
2.在dispatch中 request = self.initialize_request(request, *args, **kwargs)
是对原生request进行了封装,

3.查看initialize_request
此时原生request被封装成了如下格式
return Request(
request,
parsers=self.get_parsers(),
authenticators=self.get_authenticators(), #用户认证
negotiator=self.get_content_negotiator(),
parser_context=parser_context
)
此时authenticators=self.get_authenticators()返回的是[auth() for auth in self.authentication_classes]#列表生成式
这个格式是实例化列表中的对象

4.继续往下是dispatch中的self.initial(request, *args, **kwargs)方法
其中调用了self.perform_authentication(request)方法
self.perform_authentication(request)中调用了user方法
在user方法中:_authenticate()
or authenticator in self.authenticators:
try:
user_auth_tuple = authenticator.authenticate(self)#在这里进行了用户认证


111




原文地址:https://www.cnblogs.com/swearBM/p/10447534.html