Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Admin - remove permissions from the list on Add/Edit Group page

I am using Django 1.8.1. This is first time I am customizing the admin screens.

There are some permissions coming by default in the list like admin|entry log, auth|group etc. I wanted to remove those permissions from the list. I was able to filter those permissions out from the list on the user edit screen with the help of Django admin - change permissions list

My problem is add/edit group screen. On this screen those permission are still there. Following is the screen shot of the page. enter image description here

How can I remove some of the permissions from this list ?

like image 904
Niraj Chapla Avatar asked Dec 28 '15 04:12

Niraj Chapla


People also ask

How do I restrict access to parts of Django admin?

Django admin allows access to users marked as is_staff=True . To disable a user from being able to access the admin, you should set is_staff=False . This holds true even if the user is a superuser. is_superuser=True .

How can I remove extra's from Django admin panel?

You can add another class called Meta in your model to specify plural display name. For example, if the model's name is Category , the admin displays Categorys , but by adding the Meta class, we can change it to Categories . Literally saved my life!

How do I give permission to a specific user in Django?

through django-adminopen your django-admin page and head to Users section and select your desired user . NOTE: Permission assigning process is a one-time thing, so you dont have to update it every time unless you need to change/re-assign the permissions.


1 Answers

I tried to do this by extending the GroupAdmin class and overriding the get_form method. But unfortunately that didn't worked for me. The get_form method was not getting invoked at all. I don't know the reason.

Following solved my problem:

class MyGroupAdminForm(forms.ModelForm):

    class Meta:
        model = Group
        fields = ('name', 'permissions')

    permissions = forms.ModelMultipleChoiceField(
        Permission.objects.exclude(content_type__app_label__in=['auth','admin','sessions','users','contenttypes']),
        widget=admin.widgets.FilteredSelectMultiple(_('permissions'), False))

class MyGroupAdmin(admin.ModelAdmin):
    form = MyGroupAdminForm
    search_fields = ('name',)
    ordering = ('name',)

admin.site.unregister(Group)
admin.site.register(Group, MyGroupAdmin)

With the above code I got success to remove the permissions for apps [auth, admin, sessions, users, contenttypes] from the available permissions list on the add/edit group screen.

like image 78
Niraj Chapla Avatar answered Sep 19 '22 02:09

Niraj Chapla