My understanding and experience is that request.user
in Django returns an object of type SimpleLazyObject
. How do I obtain an object of type User
using this SimpleLazyObject
In particular this is my derived User
model
models.py
class UserInfo(User):
mobile_number = models.CharField(max_length=10, null=False, blank=False)
purchase_code = models.CharField(max_length=25, null=False, blank=False)
REQUIRED_FIELDS = ['first_name', 'last_name', 'mobile_number', 'password', 'purchase_code']
def clean(self):
self.username = str(self.mobile_number)
I have written the following function which seems to work perfectly fine except that I have to make a database query. Is there a better way of getting an instance of the currently logged in User
?
# This is a function to obtain the current logged in user. In case no one has logged-in the function
# returns an instance of the class AnonymousUser
def get_user(request):
current_user = request.user #auth.get_user(request)
if type(current_user) is not AnonymousUser:
current_user = UserInfo.objects.get(pk=auth.get_user(request).pk)
return HttpResponse(current_user)