如何在 django 中创建组权限

Posted

技术标签:

【中文标题】如何在 django 中创建组权限【英文标题】:how to create a group permission in django 【发布时间】:2013-09-18 19:22:12 【问题描述】:

我正在尝试在 auth_group_permission 表中创建一行。

我尝试了以下方法:

group_permission = group_permissions.add(group=group, permission=permission)

group_permission = group.group_permissions_set.add(permission=permission)

group_permission = group.permissions_set.add(permission=permission)

这些都不起作用。有谁知道如何向该表添加记录?

【问题讨论】:

【参考方案1】:

以下answer 帮助我建立了群组。

from django.contrib.auth.models import User, Group, Permission
from django.contrib.contenttypes.models import ContentType

content_type = ContentType.objects.get(app_label='myapp', model='BlogPost')
permission = Permission.objects.create(codename='can_publish',
                                       name='Can Publish Posts',
                                       content_type=content_type)
user = User.objects.get(username='duke_nukem')
group = Group.objects.get(name='wizard')
group.permissions.add(permission)
user.groups.add(group)

你可以通过shellpython manage.py shell添加这个权限,然后输入上面的代码。

【讨论】:

@alirezasanaee 我认为在你看来 感谢您的回答。文档对此并不十分清楚 注意django 3.x 开始,model 关键字需要小写的型号名称。【参考方案2】:
# Contain number of permission list in your system ex can_add_address, can_add_address
class Permissions(models.Model):
    permission_name = models.CharField(max_length=255, help_text="permission key")
    description = models.CharField(max_length=255, help_text="permission description")
    created_by = models.ForeignKey(User, db_index=True)
    created_on = models.DateTimeField(auto_now_add=True, help_text="Date when Role is added.")
    updated_on = models.DateTimeField(auto_now=True, help_text="Date when Role is modified.")
    enabled = models.BooleanField(default=True, help_text="Enable/Disable this object")

# create multiple group 
class RoleGroup(models.Model):
    name = models.CharField(max_length=255, help_text="Short title of Role")
    created_by = models.ForeignKey(User, db_index=True)
    created_on = models.DateTimeField(auto_now_add=True, help_text="Date when Role is added.")
    updated_on = models.DateTimeField(auto_now=True, help_text="Date when Role is modified.")
    enabled = models.BooleanField(default=True, help_text="Enable/Disable this object")

# add group permisson name with RoleGroup ex, HR name group cantaion multiple permissions
class RolePermission(models.Model):
    role_group = models.ForeignKey(RoleGroup, related_name='role_permission_group_name')
    permission_name = models.CharField(max_length=255, help_text="Short title of permission ex. can_add_address")

# Assign Multiple group to user 
class UserGroup(models.Model):
    user = models.ForeignKey(User, db_index=True, related_name='role_group')
    role_group = models.ForeignKey(RoleGroup, related_name='role_group_name', null = True, blank = True)
    created_by = models.ForeignKey(User, db_index=True)
    created_on = models.DateTimeField(auto_now_add=True, help_text="Date when Role is added.")
    updated_on = models.DateTimeField(auto_now=True, help_text="Date when Role is modified.")
    enabled = models.BooleanField(default=True, help_text="Enable/Disable this object")


#create has_permission static method in Util class
class Util :
@staticmethod
def has_permission(user, permission_name) :
    role_group = UserGroup.objects.filter(user = user).values_list('role_group_id', flat = True)
    permission_names = RolePermission.objects.filter(role_group_id__in = role_group).values_list('permission_name', flat = True)
    if permission_name in permission_names:
        return True
    return False



#Call Permisson Methods 
if Util.has_permission(request.user, 'can_edit_address') :
    // Not have permisson code  
else :
    // Have permisson code  

【讨论】:

以上是关于如何在 django 中创建组权限的主要内容,如果未能解决你的问题,请参考以下文章

在 ASmack 中创建组

在 SQL 中创建组 ID

在 Drupal 中创建组结构

在 Xcode 中创建组与创建文件夹引用

根据值从表中创建组/块

如何在 Django Guardian 中为用户定义默认权限?