Created
March 29, 2014 16:57
-
-
Save bee-keeper/9857973 to your computer and use it in GitHub Desktop.
Django: Programmatically add permissions to groups
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
The correct import of ContentType is :
from django.contrib.contenttypes.models import ContentType
bellissimo, utile!!!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.