Django assign user to request in the django.contrib.auth.middleware.py by calling
request.user = SimpleLazyObject(lambda: get_user(request))
The get_user function is in the init.py file which in turns calls another get_user function in the backends.py
def get_user(self, user_id):
try:
user = UserModel._default_manager.get(pk=user_id)
except UserModel.DoesNotExist:
return None
return user if self.user_can_authenticate(user) else None
Here it is making a database call to get user by id. How can I get the user profile(related to user by a one to one field) in the same query and propagate it back so I can assign it to request.profile?
Let's use select_related() to join tables and get both profile and user in a single query, for example: