在Django中设置组/用户的权限 [英] Setting Permissions to a group/User in Django

查看:474
本文介绍了在Django中设置组/用户的权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 auth_group 表中有一个组。我需要设置一个组的权限。在 auth_permission 表中有一组权限。现在如果我需要将所有权限映射到一个组,我需要为每个权限添加不同的行,或者可以通过将所有权限添加为1& 0?

I've a group in auth_group table. I need to set permissions to a group. There are set of permissions in auth_permission table. Now If I need to map all the permissions to a group, do I need to add different rows for each permissions, or can it be done by adding all the permissions as a string of 1 & 0?

例如: - 我应该在表格中添加条目为

For eg:- Should I add entries in the table as

id,group_id,permission_id

1,1,1
2,1,2
3,1,3
4,1,4

或在那里无论如何,我可以在一个字符串中添加所有权限,例如 1234 ,每个数字表示permission_id?

or is there any way, I can add all the permissions in one string, such as 1234, each digit signifying a permission_id?

推荐答案

您可以从 Permission 获取所有权限,并将其添加到组中:

You can get all permissions from Permission model and add them to a group:

from django.contrib.auth.models import Group, Permission

permissions = Permission.objects.all()
auth_group = Group.objects.get(...)
auth_group.permissions.add(*permissions)

这篇关于在Django中设置组/用户的权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆