在 Django 1.5 中使用多个 AUTH_USER_MODEL 的正确方法是什么?

2022-01-25 00:00:00 python django django-models django-admin

问题描述

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

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.

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

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'

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

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?


解决方案

如果您的要求是将管理员用户和客户分开,我认为拥有多个用户模型没有任何问题.在这一点上,客户模型就像任何模型一样,除了它与用户模型非常相似,如果它适合你,那就太好了.唯一的缺点是您可能必须复制 django 为 Django 用户模型提供的许多帮助程序,例如身份验证后端或用户会话.如果你愿意做这一切,这似乎完全没问题.

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.

如果您希望使用许多 django 助手,您可能需要创建一个非常基本的用户模型,作为管理员和客户的基础:

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

这将允许您重用 Django 提供的许多开箱即用的东西,但它会产生一些额外的数据库开销,因为必须计算更多的连接.但是你可以为客户缓存一些东西,这样你就可以恢复一些性能.

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.

相关文章