我创建了一群堕落的链接的小组:
我订阅了用户类添加了一个名为“角色”的选择字段。基本上,这意味着用户的角色。这样,如果用户扮演“员工”的角色。因此,它将拥有团队工作人员的许可。问题是:我无法让Django按照我的预期做出回应。保存后,我发出了一个信号,该信号应根据他们的角色将用户添加到组中。该程序看起来像这样:
# project/app/model.py
class User(AbstractUser):
class Roles(models.IntegerChoices):
SUPER = 0, _('SuperAdmins')
COMPANY = 1, _('Company')
UNITY = 2, _('Unity')
STAFF = 3, _('Staff')
role: Roles = models.IntegerField(choices=Roles.choices, default=Roles.STAFF, verbose_name=_("Role"))
我的信号就像:
GROUPS = ['SuperAdmins', 'Company', 'Unity', 'Staff']
@receiver(post_save, sender=User)
def user(sender: User, instance: User, created: bool, **kwargs) -> None:
"""
This receiver function will set every staff pages that is created to the group staff.
:param sender: the model that will trigger this receiver
:param instance: the instance
:param created: if it was already created
:param kwargs:
:return: None
"""
if created:
group = Group.objects.get(name=GROUPS[instance.role])
instance.groups.add(group)
else:
group = Group.objects.get(name=GROUPS[instance.role])
instance.groups.add(group)
print("update")
当我转到管理页面并创建一个新用户时,一切都按预期工作。但是,当我编辑现有用户的角色时,它只是在打印功能上打印“更新”,但没有任何变化。我在做什么错?
I created a bunch of groups fallowing this link: https://stackoverflow.com/questions/22250352/programmatically-create-a-django-group-with-permissions#:~:text
I subscribed the User class adding a choice field called "role". Basically, that means what role that user has. That way if a user has the role of "staff" . Thus It'll have permissions of the group staff. The problem is: I can't get django to respond the way I expected. I put a signal after saving that it should add the user to the group according to their role. The program looks like this:
# project/app/model.py
class User(AbstractUser):
class Roles(models.IntegerChoices):
SUPER = 0, _('SuperAdmins')
COMPANY = 1, _('Company')
UNITY = 2, _('Unity')
STAFF = 3, _('Staff')
role: Roles = models.IntegerField(choices=Roles.choices, default=Roles.STAFF, verbose_name=_("Role"))
My signal is like:
GROUPS = ['SuperAdmins', 'Company', 'Unity', 'Staff']
@receiver(post_save, sender=User)
def user(sender: User, instance: User, created: bool, **kwargs) -> None:
"""
This receiver function will set every staff pages that is created to the group staff.
:param sender: the model that will trigger this receiver
:param instance: the instance
:param created: if it was already created
:param kwargs:
:return: None
"""
if created:
group = Group.objects.get(name=GROUPS[instance.role])
instance.groups.add(group)
else:
group = Group.objects.get(name=GROUPS[instance.role])
instance.groups.add(group)
print("update")
When I go to the admin page and I create a new user, everything works as expected. But when I edit the role of an existing user, it just prints the "update" fron the print function but nothing changes. What am I doing wrong?
发布评论
评论(2)
如果有人遇到同样的问题,我将在此处发布解决方案。以及@mtzd的贡献。我根据本文中的问题的描述找到了解决方案的提示: https://stackoverflow.com/a/ 1925784/18600263 。因此,pos_save信号将是:
I'll post the solution here in case anyone encounters the same problem. Along with the contribution of @mtzd. I found a tip for the solution based on the description of a problem that was in this post: https://stackoverflow.com/a/1925784/18600263. So the pos_save signal will be:
使用当前的代码,更改用户角色时,它将被添加到新组中(而不会从上一组中删除)。例如,如果用户最初是组的一部分
员工
,则其角色更改为Company
,它将被添加到Company
组中。由于user
模型与组
模型之间的关系是很多与许多关系的关系,因此不会从staff
组中删除用户。为此,有一些方法(基本上是相同的):clear
然后 add :第二个选项::
set set </
code :这,如果您决定使用第二个选项,则可以使用
set
也适用于正在创建的用户:因此,您不必使用
创建的
参数。有关clear
和set
的更多信息,您可以读取docs https://docs.djangoproject.com/en/4.0/ref/ref/ref/models/models/relations/relations/#django.db.models。 fields.recated.releatedmanager.clear 看看如何使用它们!With your current code, when changing the user role it will be added to a new group (without being removed from the previous one). For example if the user is part of the group
Staff
initially and then its role changes toCompany
it will be added to theCompany
group. As the relationship between theUser
model and theGroup
model is a Many to Many relationship, the user won't be removed from theStaff
group. To do that there are to ways (which are basically the same):First option with
clear
thenadd
:Second option with
set
:With all this, if you decide to go with the second option, you can go with
set
also for users that are being created:and thus you don't have to use the
created
param. For more info onclear
andset
you can read the docs https://docs.djangoproject.com/en/4.0/ref/models/relations/#django.db.models.fields.related.RelatedManager.clear and see how they can be used!