I noticed there are two ways to get a user object from request (assuming user is already logged in and the session is valid):
user = request.user
user = get_user(request)
whereget_user()
is imported fromdjango.contrib.auth
.
What's the difference? get_user()
seems to do a lot of validation for request session.
Which is better?
CodePudding user response:
request.user
is actually populated with get_user(request)
.
It happens in AuthenticationMiddleware
:
request.user = SimpleLazyObject(lambda: get_user(request))
So when you get request.user
, you are actually getting get_user(request)
result.