Remove (or hide) default Permissions from Django

前端 未结 8 1193
醉酒成梦
醉酒成梦 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条回答
  •  天涯浪人
    2021-01-30 18:31

    If you are creating your own user management backend and only want to show your custom permissions you can filter out the default permissions by excluding permission with a name that starts with "Can".

    WARNING: You must remember not to name your permissions starting with "Can"!!!! If they decide to change the naming convention this might not work.

    With credit to pmdarrow this is how I did this in my project:

    from django.contrib.auth.forms import UserChangeForm
    from django.contrib.auth.models import Permission
    from django.contrib import admin    
    
    class UserEditForm(UserChangeForm):
        class Meta:
            model = User
    
            exclude = (
                       'last_login',
                       'is_superuser',
                       'is_staff',
                       'date_joined',
                       )
    
        user_permissions = forms.ModelMultipleChoiceField(
            Permission.objects.exclude(name__startswith='Can'), 
            widget=admin.widgets.FilteredSelectMultiple(_('permissions'), False))
    

提交回复
热议问题