0

When I first run manage.py migrate on a Django project, I would like the database to be initialized with some extra permissions. I have seen here that this can be done at the app level, in the apps.py files. However the permissions I would like to have are rather related to the whole project than to one of its apps.

My questions are:

  • Is it OK to define project-level permissions, or are permissions meant to always be part on an app?
  • If project-level permissions are fine, what would be the appropriate place to define them so that they are created at project initialization?
mimo
  • 2,469
  • 2
  • 28
  • 49

1 Answers1

0

To create a permission you need a ContentType and they live in the apps, so you can't create "project-level" permissions. The permissions must to be created in some app (everything is an app) and after install the app in your project you will be able to reference to the permission in the whole project.

As the permission is related to a ContentType (a model) you can create the permission inside you models like this:

class SomeModelClass(models.Model):
    # ...
    class Meta:
        permissions = (
            ("can_drive", "Can drive"),
            ("can_vote", "Can vote in elections"),
            ("can_drink", "Can drink alcohol"),

Note: all the models have 4 default permissions

The other way to create you permission can be programatically somewhere, maybe a signal, a custom migration... take a look here

Yasiel Cabrera
  • 540
  • 4
  • 11