Skip to content Skip to sidebar Skip to footer

Django Group Permissions For Whole App

Can I give a group all permissions for a whole application programmatically? Of course I could give the group all add,change,delete permissions for every specific model in the appl

Solution 1:

This is quick sketch of management command that syncs app models permissions with group:

# coding=utf-8from django.core.management.base import BaseCommand, CommandError
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import Permission, Groupclass Command(BaseCommand):
    args = '<group_name app_label>'
    help = ('Syncs permissions of group with given name with permissions of all models of app with giver app_label ')

    def handle(self, *args, **options):
        group_name= args[0]
        app_label = args[1]

        group = Group.objects.get(name=group_name)
        cts = ContentType.objects.filter(app_label=app_label)
        perms = Permission.objects.filter(content_type__in=cts)
        group.permissions.add(*perms)

Solution 2:

You could make the users of the group superusers, then when you check permissions, do:

if user.is_superuser:#allowelse:#check permissions

Post a Comment for "Django Group Permissions For Whole App"