如何在Django上由管理员和客户分隔用户(模型) [英] How to separate users (models) by admins and customers on Django
问题描述
我想将Django应用的用户分为两类:
- 管理(使用Django管理员的用户) - 继承自 AbstractUser
- 用户(客户用户) - 继承自 AbstractBaseUser
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
我想分开这两种用户,因为 AbstractUser
( is_staff
, is_superuser
,组
,权限
)对于我的客户用户和权限和组,我只想实现不同的东西。为什么,我想使用 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
.
但是对于django管理员用户, AbstractUser
class,它只是完美的,特别是使用权限功能。
But for django admin users, AbstractUser
class, it's just perfect and particularly with permissions feature.
class Admin(AbstractUser):
pass
class Customer(AbstractBaseUser):
pass
但现在,是否有一种方法可以精确地使用用户模型,仅用于django管理员使用 Admin
并为其余的应用使用客户
模型。
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.
我必须实现这个从头开始:
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
有了这个实现,如果我将 AUTH_USER_MODEL
设置为用户
,权限将不起作用,因为 User
没有权限
, is_superuser
和 is_staff
字段。
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.
如果将其设置为 Admin
,我将无法验证客户
与 django.contrib.auth
。
And if a set it to Admin
, I will not be able to authenticate Customers
with django.contrib.auth
.
那么你们有没有解决这个问题?
So guys do you have a solution to this issue?
推荐答案
Django提供的方式您似乎更加灵活和未来适应。
The way Django offers to you seems to be much more flexible and future-adapted.
- 您有一个内置的
用户
model,可以覆盖。无论如何,该模型具有权限,组等。 - 如果您需要不同类型用户的不同字段集,则创建一个
OneToOne
个人资料模型。 - 您的管理员(实际上是员工用户)和普通客户之间的分隔点是
User.is_staff
属性。
- You have a built-in
User
model, which you can override. Anyway, that model has permissions, groups, etc. - If you need different field sets for different kinds of users, you create a
OneToOne
profile models. - The separation point between your admins (actually, staff users) and regular customers is a
User.is_staff
attribute.
这样您就可以获得一堆酷的东西(与两个完全不同的用户模型相比):
This way you gain a bunch of cool stuff (compared to two completely different user models):
- 一切都是开箱即用的:
contrib.auth
和contrib.admin
模块。 - 易于自定义的分隔点:只需覆盖
admin_site.has_permission()
/在这里你去。 - 您有能力(但不是义务)创建客户和管理员的用户。
- 您可以向客户分配组和权限(与管理员不同)。即使你现在不需要,谁知道。
- Everything works out of the box:
contrib.auth
andcontrib.admin
modules. - Easy-customisable separation point: just override the
admin_site.has_permission()
and here you go. - 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.
也就是说,开销是可以忽略的比较好处。我强烈建议您继续使用Django的默认用户
模型,并根据需要进行扩展。
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上由管理员和客户分隔用户(模型)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!