Skip to content

Instantly share code, notes, and snippets.

@avidal
Created January 14, 2011 21:26
Show Gist options
  • Save avidal/780263 to your computer and use it in GitHub Desktop.
Save avidal/780263 to your computer and use it in GitHub Desktop.
This snippet will get all permission objects for a specific model.
from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType
from myapp.models import MyModel
content_type = ContentType.objects.get_for_model(MyModel)
permissions = Permission.objects.filter(content_type=content_type)
# permissions will be a list of Permissions objects
# if you want to get a specific one of the add/change/delete permissions:
permission = Permission.objects.filter(content_type=content_type, codename__startswith='change_')
# replace change_ with add_ or delete_ for the other permissions
# and an alternative method:
perm_name = MyModel._meta.get_add_permission()
perm = Permission.objects.get(codename=perm_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment