request.user返回一个SimpleLazyObject,我如何“唤醒”它? [英] request.user returns a SimpleLazyObject, how do I "wake" it?

查看:190
本文介绍了request.user返回一个SimpleLazyObject,我如何“唤醒”它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下方法:

def _attempt(actor):
    if actor.__class__ != User:
        raise TypeError

从视图调用哪个:

self.object.attempt(self.request.user)

正如你所看到的,_attempt方法期望actor类型为 django.contrib.auth.models.User ,但是对象似乎是的类型为 django.utils.functional.SimpleLazyObject 。为什么这样呢更重要的是,如何将 LazyObject (其显然是用户对象的一种包装器)转换为用户对象?

As you can see, the _attempt method expects actor to be type django.contrib.auth.models.User, however the object appears to be of type django.utils.functional.SimpleLazyObject. Why is this so? And more importantly, how can I convert the LazyObject (which apparently is a kind of wrapper for a User object) into a User object?

有关 Request.user 的更多信息,请访问: https://docs.djangoproject.com/en/dev/ref/request- response /#django.http.HttpRequest.user 此文档似乎表示 request.user 应该是一个用户 object ...

More info on Request.user is available here: https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest.user This documentation seems to indicate the request.user should be a User object...

======后期编辑=====

======Post-edit=====

我现在有以下方法:

def _attempt(obj, action, actor, msg): 
    actor.is_authenticated() 
    if isinstance(actor, LazyObject): 
        print type(actor) 

我传递一个用户,但是如果条件仍然为真,则 actor仍然是一个 LazyObject 。为什么是这样?

I'm passing a user, however the if condition is still true, actor is still a LazyObject. Why is this so?

推荐答案

请参阅我对类似问题的回答

Django延迟加载 request.user ,以便它可以根据身份验证状态,可以是用户 AnonymousUser 。它只在唤醒并在访问属性时返回相应的类。不幸的是, __ class __ 不计算,因为这是一个原始类属性。有时您可能需要知道这实际上是一个 SimpleLazyObject 类型,因此将代理代替到用户 AnonymousUser

Django lazy loads request.user so that it can be either User or AnonymousUser depending on the authentication state. It only "wakes up" and returns the appropriate class when an attribute is accessed on it. Unfortunately, __class__ doesn't count because that's a primitive class attribute. There's occasions where you might need to know that this is actually a SimpleLazyObject type, and therefore it would be wrong to proxy it on to User or AnonymousUser.

长而短,你根本无法做到这一点,它。但是,你真的想在这里实现什么?如果您要检查是否为用户 AnonymousUser ,则有 request.user例如.is_authenticated()。例如,

Long and short, you simply can't do this comparison as you have it. But, what are you really trying to achieve here? If you're trying to check if it's a User or AnonymousUser, there's request.user.is_authenticated() for that, for example.

作为一般规则,您不应滥用鸭式打字。一个参数应该始终是一个特别的类型或子类型( User UserSubClass ),即使它不是有。否则,你最终会出现令人困惑和脆弱的代码。

As a general rule though, you shouldn't abuse duck typing. A parameter should always be a particularly type or subtype (User or UserSubClass), even though it doesn't have to be. Otherwise, you end up with confusing and brittle code.

这篇关于request.user返回一个SimpleLazyObject,我如何“唤醒”它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆