Last active
September 11, 2016 06:47
-
-
Save bluethon/9bd9588d8ea3238a187f85cf36a69581 to your computer and use it in GitHub Desktop.
group user get_or_create contenttype
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
# http://stackoverflow.com/questions/22250352/in-django-how-do-you-programmatically-create-a-group-with-permissions | |
from django.contrib.auth.models import Group, Permission | |
from django.contrib.contenttypes.models import ContentType | |
from api.models import Project | |
new_group, created = Group.objects.get_or_create(name='new_group') | |
# Code to add permission to group ??? | |
ct = ContentType.objects.get_for_model(Project) | |
# Now what - Say I want to add 'Can add project' permission to new_group? | |
permission = Permission.objects.create(codename='can_add_project', name='Can add project', content_type=ct) | |
new_group.permissions.add(permission) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment