问题
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