Created
January 14, 2011 21:26
-
-
Save avidal/780263 to your computer and use it in GitHub Desktop.
This snippet will get all permission objects for a specific model.
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
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
can you solve this https://stackoverflow.com/questions/49584640/permission-duplicate-key-value-violates-unique-constraint-auth-permission-conte