Setting Permissions to a group/User in Django

早过忘川 提交于 2020-05-15 06:10:13

问题


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

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


回答1:


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)


来源:https://stackoverflow.com/questions/20749328/setting-permissions-to-a-group-user-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!