[英]How to handle multiple dynamic user types in Django?
I have a problem I have been stuck on for days now and can't seem to find the solution which is becoming very frustrating.我有一个问题,我已经坚持了好几天了,似乎找不到解决方案,这变得非常令人沮丧。
In my models I have Companies
.在我的模型中,我有Companies
。 Each company has 2 Group
's for authentication and authorization: Supervisor and Non-Supervisor .每个公司有 2 个Group
用于认证和授权: Supervisor 和 Non-Supervisor 。
Of these 2 Group
's there will be several dynamic sub-groups for each company, which I call Employee Type
.在这 2 个Group
中,每个公司都会有几个动态子组,我称之为Employee Type
。 These EmployeeType
's will be able to be created by the Company Admin within the Admin panel then assigned to one of the Group
s.这些EmployeeType
将能够由公司管理员在管理面板中创建,然后分配给其中一个Group
。
To summarize: Each Company
will have 2 Group
s.总结一下:每家Company
将有 2 个Group
。 Each Group
will have unlimited # of EmployeeType
s.每个Group
将有无限的EmployeeType
。 Each EmployeeType
will have unlimited # of Users
.每个EmployeeType
将有无限的Users
数。
Even though the Group
s are similar across all companies, the EmployeeType
s can have a similar name but should not be able to be shared across companies.尽管Group
s 在所有公司中都是相似的,但EmployeeType
s 可以具有相似的名称,但不应在公司之间共享。
class Company(models.Model):
....
class EmployeeType(models.Model):
....
employee_type = models.CharField(
max_length=32,
default='Server'
)
group = models.ForeignKey(
Group,
on_delete=models.CASCADE,
null=True
)
class User(AbstractUser):
....
company = models.ForeignKey(
Company,
on_delete=models.CASCADE,
null=True
)
employee_type = models.ForeignKey(
EmployeeType,
on_delete=models.CASCADE,
null=True,
)
Company A
can have their own EmployeeType
s and Company B
can have their own EmployeeType
s but the name of an EmployeeType
in both companies can be the same while also making sure that Company A
cannot see Company B
's EmployeeType
s我怎样才能使Company A
可以拥有自己的EmployeeType
, Company B
可以拥有自己的EmployeeType
,但是两家公司中EmployeeType
的名称可以相同,同时还要确保Company A
看不到Company B
的EmployeeType
Apologies if it seems very simple but I am confused when the EmployeeType
s become dynamic.抱歉,如果它看起来很简单,但是当EmployeeType
变为动态时我很困惑。 I end up having circular logic when I try this and can't seem to wrap my head around the correct structure of it.当我尝试这个时,我最终会遇到循环逻辑,并且似乎无法理解它的正确结构。
Let me answer your questions all at once.让我一次性回答你的问题。
Is this the correct way to model the dynamic user type creation?这是 model 动态用户类型创建的正确方法吗? NO不
Group/Supervisor
should be a BooleanField
in EmployeeType
. Group/Supervisor
应该是EmployeeType
中的BooleanField
。 It need not be a separate model and a foreign key.它不必是单独的 model 和外键。 It will make your authentication and authorization functions much easier since from what I understand they are only of supervisor True/False nature这将使您的身份验证和授权功能更加容易,因为据我了解,它们仅具有主管 True/False 的性质
User
should have EmployeeType
as a ManyToManyField
since you might want to add More roles/EmployeeTypes to an Employee in the future. User
应该将EmployeeType
作为ManyToManyField
,因为您将来可能希望向 Employee 添加更多角色/EmployeeTypes。
You do not need Company
ForeignKey
in User since it will be taken care of through EmployeeType
ManytoManyField
Relations您不需要在 User 中使用Company
ForeignKey
,因为它将通过EmployeeType
ManytoManyField
关系进行处理
Add Company ForeignKey
in EmployeeType
and since you would be removing Group Foreign Key
from Company, EmployeeType and User
在EmployeeType
中添加Company ForeignKey
外键,因为您将从Company, EmployeeType and User
中删除Group Foreign Key
So your models should look like this:所以你的模型应该是这样的:
class Company(models.Model):
....
class EmployeeType(models.Model):
....
employee_type = models.CharField(
max_length=32,
default='Server'
)
company = models.ForeignKey(
Company,
on_delete=models.CASCADE,
null=True
)
supervisor = models.BooleanField(default=False)
class User(AbstractUser):
....
employee_type = models.ManyToManyField(
EmployeeType,
null=True,
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.