[Fixed]-Django Check and set permissions for a user group

26👍

Take a look at this blog post: Django: Using The Permission System

Adapted to your example:

can_fm_list = Permission.objects.get(name='can_fm_list')
newgroup.permissions.add(can_fm_list)
👤arie

8👍

For others who are looking for adding multiple permissions at once,

permissions_list = Permission.objects.all()
new_group.permissions.set(permissions_list)

0👍

list of permissions add to a new group :-

group_name = request.POST.get('group_name')
permissions_list = request.POST.getlist('permission')
permissions_list_id = []
new_group = Group.objects.create(name=group_name)

for perm in permissions_list:
    p = Permission.objects.get(name=perm)
    permissions_list_id.append(p.id)

new_group.permissions.set(permissions_list_id)

Leave a comment