I use a proxy model on User
like
class Nuser(User):
class Meta:
proxy = True
def __unicode__(self):
return u'%s %s' % (self.first_name, self.last_name)
I use it throughout my views.
I was wondering the best way to get the instance of this object for the request.user
Each time I do
Nuser.objects.get(pk=request.user.pk)
Isn't there a simpler way to do it ?
You could write a custom authentication backend that returns instances of your proxy model instead of a User
instance:
from django.contrib.auth.backends import ModelBackend
class ProxiedModelBackend(ModelBackend):
def get_user(self, user_id):
try:
return Nuser.objects.get(pk=user_id)
except Nuser.DoesNotExist:
return None
In your settings.py
AUTHENTICATION_BACKENDS = ['my_project.auth_backends.ProxiedModelBackend',]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With