我正在django中研究用户/角色/权限模型,其中的模型结构是这样。
class GlobalPermission(BasicAbstractClass):
TYPE_CHOICES = ["user_level", "platform_level", "user_platform_access_level"]
short_code = models.CharField(max_length=64, blank=True, null=True)
name = models.CharField(max_length=92, blank=True, null=True)
description = models.CharField(max_length=189, blank=True, null=True)
type = EnumField(choices=TYPE_CHOICES, blank=True, null=True)
def __str__(self):
return self.name
class Meta:
db_table = 'global_permission'
class Role(BasicAbstractClass):
ROLE_CHOICES = ["admin", "analyst", "moderator", "editor", "superuser", "developer"]
SERVICE_CHOICES = ["social_media_management", "survey"]
service = EnumField(choices=SERVICE_CHOICES)
name = EnumField(choices=ROLE_CHOICES)
permissions = models.ManyToManyField(GlobalPermission, related_name="role_permissions", blank=True, through="RolePermission")
def __str__(self):
return self.name + " of " + self.service
class Meta:
db_table = 'role'
class RolePermission(models.Model):
role = models.ForeignKey(Role, on_delete=models.CASCADE)
permission = models.ForeignKey(GlobalPermission, on_delete=models.CASCADE)
def __str__(self):
return str(self.id)
class Meta:
db_table = "role_permissions"
class UserAccount(models.Model):
roles = models.ManyToManyField(Role, related_name="+", blank=True, through="UserRole")
class UserRole(BasicAbstractClass):
user = models.ForeignKey(UserAccount, on_delete=models.CASCADE)
role = models.ForeignKey(Role, on_delete=models.CASCADE)
def __str__(self):
return str(self.id)
class Meta:
db_table = "user_role"
我在这里想要的是获取用户权限列表。在这里,可以为用户分配多个角色,并且我想要一个包含这些角色的所有权限的查询集。
答案 0 :(得分:1)
从您想要的东西(GlobalPermission
)开始,然后在查询中向后工作:
GlobalPermission.objects.filter(role_permissions__user_accounts=target_user).distinct()
请注意,您需要一种在查询中引用UserAccount
的方式,例如:
class UserAccount(models.Model):
roles = models.ManyToManyField(Role, blank=True, through="UserRole",
related_name="+", related_query_name="user_accounts")