Skip to content

Instantly share code, notes, and snippets.

@bluethon
Last active September 11, 2016 06:47
Show Gist options
  • Save bluethon/9bd9588d8ea3238a187f85cf36a69581 to your computer and use it in GitHub Desktop.
Save bluethon/9bd9588d8ea3238a187f85cf36a69581 to your computer and use it in GitHub Desktop.
group user get_or_create contenttype
# 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