Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bee-keeper/9857973 to your computer and use it in GitHub Desktop.
Save bee-keeper/9857973 to your computer and use it in GitHub Desktop.
Django: Programmatically add permissions to groups
content_type = ContentType.objects.get(app_label='', model='')
#get all permssions for this model
perms = Permission.objects.filter(content_type=content_type)
group = Group.objects.get(name='')
for p in perms:
group.permissions.add(perms)
@meteozond
Copy link

There is typo:

for p in perms:
-  group.permissions.add(perms)
+  group.permissions.add(p)

@bastien34
Copy link

Helpful tips, thanks. You could add:

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

and precise that the parameter model is the lowered model name.

@Papiex
Copy link

Papiex commented Oct 6, 2022

The correct import of ContentType is :

from django.contrib.contenttypes.models import ContentType

@IlRatto
Copy link

IlRatto commented May 22, 2023

bellissimo, utile!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment