简体   繁体   中英

How to add a custom permission to a group in Django?

I think I have coded a custom permission in a model:

class Foo(models.Model):
    class Meta:
        permissions = [('admin_foo', 'Can administrate foo'),]
    ...

I ran the commands to store the permission:

python manage.py makemigrations
python manage.py migrate

Then I have a decorator on my protected view:

@permission_required('app.admin_foo')
def myFooView(request):
    ...

How do I add this permission to the group "bar" programmatically?

permission = Permission.objects.get(codename=perm_name)
group.permissions.add(permission)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM