How to retrieve all permissions of a specific model in django?
you can check on codename
field which will be something like: 'change_company'
etc ...
model_name = 'company'
all_perms_on_this_modal = Permission.objects.filter(codename__contains=model_name)
I would suggest you something like this:
all_permissions = Permission.objects.filter(content_type__app_label='app label', content_type__model='lower case model name')
Retrieving model's app_label
:
Company._meta.app_label
Retrieving model's lower case name:
Company._meta.model_name
Also, you can retrieve a ContentType
instance representing a model:
ContentType.objects.get_for_model(Company)
Since ContentType
uses a cache, it is quite acceptable. Thus, there is another way to achieve what you need:
content_type = ContentType.objects.get_for_model(Company)
all_permissions = Permission.objects.filter(content_type=content_type)