在Django 1.5中使用多个AUTH_USER_MODEL的正确方法是什么? [英] What's the proper way to use multiple AUTH_USER_MODEL in Django 1.5?

查看:668
本文介绍了在Django 1.5中使用多个AUTH_USER_MODEL的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为django.contrib.auth模块使用两种不同的模型。第一个是Django提供的默认用户模型,它完全适用于管理员访问(组,权限等),但另一个是具有很多不同属性(城市,区域设置,地址等)的客户模型,与默认用户模型。这些用户组必须使用不同的表,不能有任何关系。



我创建了一个继承自AbstractBaseUser的Customer模型和一个名为ChangeBaseUser的中间件类,如下所示:


  class ChangeBaseUser(object):
def process_request(self,request):
match = resolve(request.path )
如果match.app_name ==myapp:
settings.AUTH_USER_MODEL ='myapp.Customer'
else:
settings.AUTH_USER_MODEL ='auth.User'

它正在工作,但我不知道这是否是正确的方法,因为在文档中有一个部分(链接 )这意味着方便的方法是为默认用户模型分配一个静态值。



如果这不是正确的方法,你有什么建议havin每个模块可以使用多个用户模型?

解决方案

如果您的要求是让管理员用户和客户分开,我看不到有多个用户模型有什么问题。在这一点上,客户模型就像任何型号,除了它非常类似于用户模型,如果这适用于你,这是非常好的。唯一的缺点是您必须重复许多帮助器django为您提供Django用户模型,例如auth后端或用户会话。如果你愿意做所有这些,这似乎是完美的。



如果你希望使用许多django帮助者,你可能想要创建一个非常基本的用户模型这将作为管理员和客户的基础:

  class User(AbstractBaseUser):
# auth and sessions

class Admin(models.Model):
user = models.OneToOneField(UserBase,related_name ='admins')
#...其他管理专用字段

class Customer(models.Model):
user = models.OneToOneField(UserBase,related_name ='admins')
#...其他客户特定字段

这将允许您重用许多Django提供的开箱即用的东西,但会导致额外的额外db开销因为必须计算更多的连接。但是,您可以为客户缓存东西,以便您可以获得一些性能。


I want to use two different models for django.contrib.auth module. The first one is the default User model provided by Django which is completely suitable for admin access (groups, permissions etc.) but the other one is customer model which has a lot of different attributes (city, locale, address etc.) compared to default User model. These user groups must use different tables and mustn't have any relation.

I created a Customer model inherited from AbstractBaseUser and a middleware class called ChangeBaseUser like this:

class ChangeBaseUser(object):
    def process_request(self, request):  
        match = resolve(request.path)
        if match.app_name == "myapp":
            settings.AUTH_USER_MODEL = 'myapp.Customer'
        else:
            settings.AUTH_USER_MODEL = 'auth.User'

It's working but I'm not sure whether this is the proper way to do it because in documentation there is a section (link) that implies the convenient way is to assign a static value for default user model.

If this is not the proper way, do you have any suggestions for having multiple user models per module basis?

解决方案

If your requirement is to keep admin users and customers separate, I don't see anything wrong with having multiple user models. At this point, the customer model is like any model, except it is very similar to the user model and that is perfectly fine if that works for you. The only disadvantage is that you will have to possibly duplicate many helpers django gives you for the Django user model such as auth backend or sessions for users. If you are willing to do all that, this seems perfectly fine.

If you wish however to utilize many of the django helpers you might want to create a very basic user model which will serve as a base for both admins and customers:

class User(AbstractBaseUser):
    # use this for auth and sessions

class Admin(models.Model):
    user = models.OneToOneField(UserBase, related_name='admins')
    # ... other admin-specific fields

class Customer(models.Model):
    user = models.OneToOneField(UserBase, related_name='admins')
    # ... other customer-specific fields

This will allow you to reuse many of the things Django provides out of the box however it will incur some additional db overhead since more joins will have to be calculated. But then you can cache things for customers so you can get some of the performance back.

这篇关于在Django 1.5中使用多个AUTH_USER_MODEL的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆