Remove (or hide) default Permissions from Django

前端 未结 8 1166
醉酒成梦
醉酒成梦 2021-01-30 17:53

I\'m developing a Django app that will have two administration backends. One for daily use by \"normal\" users and the default one for more advanced tasks and for the developers

8条回答
  •  -上瘾入骨i
    2021-01-30 18:35

    I struggled with this same problem for a while and I think I've come up with a clean solution. Here's how you hide the permissions for Django's auth app:

    from django.contrib import admin
    from django.utils.translation import ugettext_lazy as _
    from django import forms
    from django.contrib.auth.models import Permission
    
    class MyGroupAdminForm(forms.ModelForm):
        class Meta:
            model = MyGroup
    
        permissions = forms.ModelMultipleChoiceField(
            Permission.objects.exclude(content_type__app_label='auth'), 
            widget=admin.widgets.FilteredSelectMultiple(_('permissions'), False))
    
    
    class MyGroupAdmin(admin.ModelAdmin):
        form = MyGroupAdminForm
        search_fields = ('name',)
        ordering = ('name',)
    
    admin.site.unregister(Group)
    admin.site.register(MyGroup, MyGroupAdmin)
    

    Of course it can easily be modified to hide whatever permissions you want. Let me know if this works for you.

提交回复
热议问题