Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Difference between request.user vs. get_user(request) in Django?

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) where get_user() is imported from django.contrib.auth.

What’s the difference? get_user() seems to do a lot of validation for request session.

Which is better?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

‍‍‍‍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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading