PermissionRequiredMixin - where do you find the "permission codename"?

Background: Trying to apply permission checks to CBVs using the PermissionRequiredMixin.

In the Django docs, it says:

Just like the has_perm() method, permission names take the form "<app label>.<permission codename>" (i.e. polls.add_choice for a permission on a model in the polls application).

Where do you find the <permission codename> part of this?

I have not established any custom permissions, however I see in the Django admin that there are already permissions related to all of the models I created. I don’t see “codenames” but for each permission there is text that goes “ABCapplabel | XYZmodelname | Can view XYZmodelname” etc. Are the permission codenames these four:

  • add
  • change
  • delete
  • view

then an underscore and then the model name? Like this?

  • ABCapplabel.add_XYZmodelname
  • ABCapplabel.change_XYZmodelname
  • ABCapplabel.delete_XYZmodelname
  • ABCapplabel.view_XYZmodelname

You’re close, the model names used are lower-case. But that is the pattern for the defaults created by migrations when models are added to Django.

You can see the codenames directly, it’s a field in the Permission model.

Also see

Got it, thanks! For this:

I tried to do this by:

  • going to the terminal with my virtual environment
  • entering python3
  • entering from django.contrib.auth.models import Permission - note: I did this thinking that after importing this, I would enter something like print(Permission.ABCapplabel.xyzmodelname.codename.all()
  • But I received a traceback before that saying this:

>>> from django.contrib.auth.models import Permission
Traceback (most recent call last):
File “”, line 1, in
File “/Users/stephaniegoulet/Desktop/ProjectA/.venv/lib/python3.10/site-packages/django/contrib/auth/models.py”, line 3, in
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File “/Users/stephaniegoulet/Desktop/ProjectA/.venv/lib/python3.10/site-packages/django/contrib/auth/base_user.py”, line 49, in
class AbstractBaseUser(models.Model):
File “/Users/stephaniegoulet/Desktop/ProjectA/.venv/lib/python3.10/site-packages/django/db/models/base.py”, line 127, in new
app_config = apps.get_containing_app_config(module)
File “/Users/stephaniegoulet/Desktop/ProjectA/.venv/lib/python3.10/site-packages/django/apps/registry.py”, line 260, in get_containing_app_config
self.check_apps_ready()
File “/Users/stephaniegoulet/Desktop/ProjectA/.venv/lib/python3.10/site-packages/django/apps/registry.py”, line 137, in check_apps_ready
settings.INSTALLED_APPS
File “/Users/stephaniegoulet/Desktop/ProjectA/.venv/lib/python3.10/site-packages/django/conf/init.py”, line 87, in getattr
self._setup(name)
File “/Users/stephaniegoulet/Desktop/ProjectA/.venv/lib/python3.10/site-packages/django/conf/init.py”, line 67, in _setup
raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

Since the traceback talks about INSTALLED_APPS, I then tried to make a new .py file and added

from django.contrib.auth.models import Permission

print(permission.ABCapplabel.xyzmodelname.codename.all()

but then realized that I don’t know how I’d make the print statement run from the file.

Can you please point me in the right direction on how to see the codename directly?

In the shell, you can get all the permissions as:
permissions = Permission.objects.all()

You can then do something like:

for perm in permissions:
  print(perm.name, "\t---\t", str(perm.content_type), "\t---\t", perm.codename)

(It’s just another model. You would work with it in exactly the same way that you work with any models you create.)

1 Like

I’m still unsure about what to do about the traceback received (see prior post) when I try to import the Permission model using this line in the shell: from django.contrib.auth.models import Permission

Otherwise, that makes a lot more sense; thank you!

You don’t run Python directly, you need to use the manage.py shell command.

Thanks so much, Ken.

For reference: This related video was also helpful to me: Using the Django Shell to Explore One-to-Many Models