Django-请求中的用户代理模型 [英] Django - User proxy model from request
本文介绍了Django-请求中的用户代理模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 User
用户上使用代理模型,例如
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)
我在整个视图中都使用它。
我想知道最好的方法是获取 request.user
I use it throughout my views.
I was wondering the best way to get the instance of this object for the request.user
每次执行此操作
Nuser.objects.get(pk=request.user.pk)
没有更简单的方法吗?
推荐答案
您可以编写一个自定义身份验证后端,该后端返回代理模型的实例而不是 User
实例:
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
在您的 settings.py
AUTHENTICATION_BACKENDS = ['my_project.auth_backends.ProxiedModelBackend',]
这篇关于Django-请求中的用户代理模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文