如何在Django上由管理员和客户分隔用户(模型)

时间:2022-09-09 07:32:50

I would like to separate users of my Django app in two classes :
- Admin (users that use Django admin) - inherit from AbstractUser
- User (customers users) - inherit from AbstractBaseUser

我想将我的Django应用程序的用户分为两类: - Admin(使用Django admin的用户) - 继承自AbstractUser - 用户(客户用户) - 继承自AbstractBaseUser

I want to separate this two kinds of users because all fields of AbstractUser (is_staff, is_superuser, groups, permissions) are useless for my customer users and for permissions and group, I just want to implement something different. That why, I want to use AbstractBaseUser.

我想将这两种用户分开,因为AbstractUser(is_staff,is_superuser,groups,permissions)的所有字段对我的客户用户以及权限和组都没用,我只想实现不同的东西。那就是为什么,我想使用AbstractBaseUser。

But for django admin users, AbstractUser class, it's just perfect and particularly with permissions feature.

但对于django管理员用户,AbstractUser类,它是完美的,特别是具有权限功能。

class Admin(AbstractUser):
    pass

class Customer(AbstractBaseUser):
    pass

But now, is there a way to precise the User model used Admin for the django admin only? And use the Customer model for the rest of my apps.

但现在,有没有办法精确地使用用户模型管理员只为django管理员?并将Customer模型用于我的其他应用程序。

Did I have to implement this from scratch :

我是否必须从头开始实施:

class MyUser(AbstractBaseUser):
    username = models.CharField(max_length=30, unique=True)
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    email = models.EmailField()
    is_active = models.BooleanField(default=False)

class Admin(MyUser, PermissionsMixin):
    is_staff = models.BooleanField(default=True)


class Customer(MyUser):
    # specific fields
    pass

With this implementation, if I set AUTH_USER_MODEL to User, permissions will not work because User has no permissions, is_superuser and is_staff fields.

通过此实现,如果我将AUTH_USER_MODEL设置为User,则权限将不起作用,因为User没有权限,is_superuser和is_staff字段。

And if a set it to Admin, I will not be able to authenticate Customers with django.contrib.auth.

如果将其设置为Admin,我将无法使用django.contrib.auth对客户进行身份验证。

So guys do you have a solution to this issue?

那么伙计们你有解决这个问题的方法吗?

1 个解决方案

#1


5  

The way Django offers to you seems to be much more flexible and future-adapted.

Django为您提供的方式似乎更灵活,适应未来。

  1. You have a built-in User model, which you can override. Anyway, that model has permissions, groups, etc.
  2. 您有一个内置的用户模型,您可以覆盖它。无论如何,该模型具有权限,组等。

  3. If you need different field sets for different kinds of users, you create a OneToOne profile models.
  4. 如果您需要为不同类型的用户设置不同的字段集,则可以创建OneToOne配置文件模型。

  5. The separation point between your admins (actually, staff users) and regular customers is a User.is_staff attribute.
  6. 管理员(实际上是员工用户)和普通客户之间的分离点是User.is_staff属性。

This way you gain a bunch of cool stuff (compared to two completely different user models):

这样你获得了很多很酷的东西(与两个完全不同的用户模型相比):

  • Everything works out of the box: contrib.auth and contrib.admin modules.
  • 一切都开箱即用:contrib.auth和contrib.admin模块。

  • Easy-customisable separation point: just override the admin_site.has_permission() and here you go.
  • 易于定制的分离点:只需覆盖admin_site.has_permission(),然后就可以了。

  • You have the ability (but not obligation) to create users which are either customers and admins.
  • 您有能力(但没有义务)创建客户和管理员用户。

  • You can assign groups and permissions (different from your admins' ones) to your customers. Even you don't need it now, who knows.
  • 您可以将组和权限(与管理员的权限不同)分配给您的客户。即使你现在也不需要它,谁知道呢。

As for drawbacks. The only one you've pointed out so far: your customers will be having (unused for now) permissions. Well, as they (as well as groups) are just separate tables, your customer data will have no performance of storage overhead.

至于缺点。到目前为止您唯一指出的一个:您的客户将拥有(暂时不使用)权限。好吧,因为它们(以及组)只是单独的表,您的客户数据将没有存储开销的性能。

That is to say, the overhead is negligeable compared to the benefits. I'd strongly recommend staying with Django's default User model and extending it if necessary.

也就是说,与收益相比,开销是可以忽略的。我强烈建议保留Django的默认用户模型并在必要时进行扩展。

#1


5  

The way Django offers to you seems to be much more flexible and future-adapted.

Django为您提供的方式似乎更灵活,适应未来。

  1. You have a built-in User model, which you can override. Anyway, that model has permissions, groups, etc.
  2. 您有一个内置的用户模型,您可以覆盖它。无论如何,该模型具有权限,组等。

  3. If you need different field sets for different kinds of users, you create a OneToOne profile models.
  4. 如果您需要为不同类型的用户设置不同的字段集,则可以创建OneToOne配置文件模型。

  5. The separation point between your admins (actually, staff users) and regular customers is a User.is_staff attribute.
  6. 管理员(实际上是员工用户)和普通客户之间的分离点是User.is_staff属性。

This way you gain a bunch of cool stuff (compared to two completely different user models):

这样你获得了很多很酷的东西(与两个完全不同的用户模型相比):

  • Everything works out of the box: contrib.auth and contrib.admin modules.
  • 一切都开箱即用:contrib.auth和contrib.admin模块。

  • Easy-customisable separation point: just override the admin_site.has_permission() and here you go.
  • 易于定制的分离点:只需覆盖admin_site.has_permission(),然后就可以了。

  • You have the ability (but not obligation) to create users which are either customers and admins.
  • 您有能力(但没有义务)创建客户和管理员用户。

  • You can assign groups and permissions (different from your admins' ones) to your customers. Even you don't need it now, who knows.
  • 您可以将组和权限(与管理员的权限不同)分配给您的客户。即使你现在也不需要它,谁知道呢。

As for drawbacks. The only one you've pointed out so far: your customers will be having (unused for now) permissions. Well, as they (as well as groups) are just separate tables, your customer data will have no performance of storage overhead.

至于缺点。到目前为止您唯一指出的一个:您的客户将拥有(暂时不使用)权限。好吧,因为它们(以及组)只是单独的表,您的客户数据将没有存储开销的性能。

That is to say, the overhead is negligeable compared to the benefits. I'd strongly recommend staying with Django's default User model and extending it if necessary.

也就是说,与收益相比,开销是可以忽略的。我强烈建议保留Django的默认用户模型并在必要时进行扩展。