Search code examples
djangousergroupsdjango-permissions

Confusion about the django permission and group model?


For example I create permission like this:

Permission.objects.create(name='Can add',codename='can_add',content_type=1)

Now if I want to apply this permission in some view I need to use permission_required decorator like this

@permission_required('app.can_add', raise_exception=True)
def some_view(request):
 ...

Here I need to exactly match the permission code_name in the decorator in order to apply the permission .

But what if admin(not developer) created new permission with different codename than the codename which is used in a view? We should go manually to the code and edit the codename ? Or is there any better solutions?How can admin apply the newly created permission in the view without manually going in the code?

I am thinking it from the normal user perspective, after we gave the project to the client.How can he/she manage such things?

Note:I am not using django default admin panel


Solution

  • Simple answer: creating custom permissions via the admin doesn't make any sense indeed since the code won't know anything about those permissions (and the permissions don't know anything about your code either FWIW).

    If your app needs custom permissions, you create them via code (ie in a migration), and deploy them together with the code that uses them. Then the admins can assign those permissions to selected users or groups as they see fit.