Remove (or hide) default Permissions from Django

前端 未结 8 1163
醉酒成梦
醉酒成梦 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:47

    You can't easily delete those permissions (so that syncdb won't put them back), but you can hide them from the admin interface. The idea is, as described by others, to override the admin forms but you have to do this for both users and groups. Here is the admin.py with the solution:

    from django import forms
    from django.contrib import admin
    from django.contrib.auth.models import Permission
    from django.contrib.auth.models import User, Group
    from django.contrib.auth.admin import GroupAdmin, UserAdmin
    from django.contrib.auth.forms import UserChangeForm
    
    #
    # In the models listed below standard permissions "add_model", "change_model"
    # and "delete_model" will be created by syncdb, but hidden from admin interface.
    # This is convenient in case you use your own set of permissions so the list
    # in the admin interface wont be confusing.
    # Feel free to add your models here. The first element is the app name (this is
    # the directory your app is in) and the second element is the name of your model
    # from models.py module of your app (Note: both names must be lowercased).
    #
    MODELS_TO_HIDE_STD_PERMISSIONS = (
        ("myapp", "mymodel"),
    )
    
    def _get_corrected_permissions():
        perms = Permission.objects.all()
        for app_name, model_name in MODELS_TO_HIDE_STD_PERMISSIONS:
            perms = perms.exclude(content_type__app_label=app_name, codename='add_%s' % model_name)
            perms = perms.exclude(content_type__app_label=app_name, codename='change_%s' % model_name)
            perms = perms.exclude(content_type__app_label=app_name, codename='delete_%s' % model_name)
        return perms
    
    class MyGroupAdminForm(forms.ModelForm):
    
        class Meta:
            model = Group
    
        permissions = forms.ModelMultipleChoiceField(
            _get_corrected_permissions(),
            widget=admin.widgets.FilteredSelectMultiple(('permissions'), False),
            help_text = 'Hold down "Control", or "Command" on a Mac, to select more than one.'
        )
    
    class MyGroupAdmin(GroupAdmin):
    
        form = MyGroupAdminForm
    
    class MyUserChangeForm(UserChangeForm):
    
        user_permissions = forms.ModelMultipleChoiceField(
            _get_corrected_permissions(),
            widget=admin.widgets.FilteredSelectMultiple(('user_permissions'), False),
            help_text = 'Hold down "Control", or "Command" on a Mac, to select more than one.'
        )
    
    class MyUserAdmin(UserAdmin):
    
        form = MyUserChangeForm
    
    admin.site.unregister(Group)
    admin.site.register(Group, MyGroupAdmin)
    admin.site.unregister(User)
    admin.site.register(User, MyUserAdmin)
    

提交回复
热议问题