我正在阅读Django的源代码,遇到了一个关于AuthenticationMiddleware
的问题。在
如文档所述,认证中间件
Adds the user attribute (a instance of
User
model) to every incoming HttpRequest
但我不明白在AuthenticationMiddleware.process_request()
中是怎么做到的。如下面的代码所示,process_request
这里只需将LazyUser()
分配给request.__class__
,这与User
模型无关。而且LazyUser.__get__()
看起来很奇怪,让我很困惑。在
class LazyUser(object):
def __get__(self, request, obj_type=None):
if not hasattr(request, '_cached_user'):
from django.contrib.auth import get_user
request._cached_user = get_user(request)
return request._cached_user
class AuthenticationMiddleware(object):
def process_request(self, request):
assert hasattr(request, 'session'), "The Django authentication middleware requires session middleware to be installed. Edit your MIDDLEWARE_CLASSES setting to insert 'django.contrib.sessions.middleware.SessionMiddleware'."
request.__class__.user = LazyUser()
return None
简单地说,我想知道当AuthenticationMiddleware
钩住处理HttpRequest
时,幕后究竟发生了什么??在
LazyUser
对象是一个Python descriptor,也就是说,一个可以指定如何通过其父类的实例访问自身的对象。(那是一口)让我看看能不能给你讲讲:这有帮助吗?:)
懒虫只是一个包装,一个懒惰的包装。get方法是Python的神奇方法之一,当访问die对象时,将调用该方法。因此,只有在实际使用时才确定用户。这是有意义的,因为此操作将触发数据库调用,并且只有在用户确实需要时才应该这样做。在
LazyUser不是分配给请求实例本身,而是分配给reqeust的实例类,这也使它对实例可用。我不知道为什么要这样做。在
相关问题 更多 >
编程相关推荐