REST Framework的 APIView 类有个方法,方法里只有一句:request.user,这个挺有歧义的。
def perform_authentication(self, request): """ Perform authentication on the incoming request. Note that if you override this and simply 'pass', then authentication will instead be performed lazily, the first time either `request.user` or `request.auth` is accessed. """ request.user
乍一看谁知道user是个属性还是方法?是属性语法也不对,那只能是方法了。重写authenticate()时,使用request.user 引用用户竟然触发了死循环。。
在Request类中找到了request.user方法:
class Request:
...
@property
def user(self):
"""
Returns the user associated with the current request, as authenticated
by the authentication classes provided to the request.
"""
if not hasattr(self, '_user'):
with wrap_attributeerrors():
self._authenticate()
return self._user