我有以下方法:
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
(显然是 User 对象的一种包装器)转换为 User
对象呢?
有关 Request.user
的更多信息,请参见:https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest.user此文档似乎表明,request.user
应该是一个 User
对象...
======编辑后的内容如下:=====
现在我有了以下方法:
def _attempt(obj, action, actor, msg):
actor.is_authenticated()
if isinstance(actor, LazyObject):
print type(actor)
我传递了一个用户,但是if
条件仍然为真,演员仍然是一个LazyObject
。为什么会这样?
`def _attempt(obj, action, actor, msg): actor.is_authenticated() if isinstance(actor, LazyObject): print type(actor)`
我正在传递一个用户,但是if条件仍然为真,actor仍然是LazyObject。为什么会这样? - Himerzi