Django auth mid的一些内部结构

2024-09-28 19:34:28 发布

您现在位置:Python中文网/ 问答频道 /正文

在django.contrib.auth公司中间件 我看到了密码:

class AuthenticationMiddleware(object):
    def process_request(self, request):
        assert hasattr(request, 'session'), "requires session middleware"
        request.__class__.user = LazyUser()
        return None

请告诉我为什么要这样的表格 请求。classclass\user=LazyUser() 用过吗? 为什么不只是 请求.用户=懒人() ? 在

我知道class属性意味着什么,但正如我理解的那样,直接赋值给实例变量会更好。我哪里错了?在


Tags: 中间件djangoauth密码objectrequestsessiondef
2条回答

这将影响requests的创建方式。所有这样的实例都将有它们的user属性作为特定的LazuUser,而不需要在每个{}实例化后进行更改。在

LazyUser是描述符类。根据documentation它只能是类属性而不是实例一:

For instance, a.x has a lookup chain starting with a.__dict__['x'], then type(a).__dict__['x'], and continuing through the base classes of type(a) excluding metaclasses.

相关问题 更多 >