In Django Admin how do I disable the Delete link

前端 未结 7 2234
天命终不由人
天命终不由人 2020-12-07 22:29

I\'ve managed to disable the \"Delete selected\" action. Easy.

But a user can still click on an item and then there\'s the red Delete link at the bottom.

相关标签:
7条回答
  • 2020-12-07 22:53

    admin.site.disable_action('delete_selected')

    From the docs

    0 讨论(0)
  • 2020-12-07 22:55

    If you want to disable an specific one that isn't custom do this. In django 1.6.6 I had to extend get_actions plus define has_delete_permission. The has_delete_permission solution does not get rid of the action from the dropdown for me:

    class MyModelAdmin(admin.ModelAdmin):
    
        ....
    
        def get_actions(self, request):
            #Disable delete
            actions = super(MyModelAdmin, self).get_actions(request)
            del actions['delete_selected']
            return actions
    
        def has_delete_permission(self, request, obj=None):
            #Disable delete
            return False
    

    Not including it in actions = ['your_custom_action'], only works for the custom actions (defs) you have defined for that model. The solution AdminSite.disable_action('delete_selected'), disables it for all models, so you would have to explicitly include them later per each modelAdmin

    0 讨论(0)
  • 2020-12-07 23:02

    The solutions here are already nice, but I prefer to have it as a reusable mixin, like this:

    class NoDeleteAdminMixin:
        def has_delete_permission(self, request, obj=None):
            return False
    

    You can use this in all your admins where you want to prevent deletion like this:

    class MyAdmin(NoDeleteAdminMixin, ModelAdmin):
        ...
    
    0 讨论(0)
  • 2020-12-07 23:03

    Simple :)

    class DeleteNotAllowedModelAdmin(admin.ModelAdmin):
        # Other stuff here
        def has_delete_permission(self, request, obj=None):
            return False
    
    0 讨论(0)
  • 2020-12-07 23:10

    Simply disable the yourapp.delete_yourmodel permission for that user or the group to which (s)he belongs.

    0 讨论(0)
  • 2020-12-07 23:11

    Well you probably are using:

    AdminSite.disable_action('delete_selected')
    

    For further control just implement your own admin and set its actions to whatever you need:

    class MyModelAdmin(admin.ModelAdmin):
        actions = ['whatever', 'actions']
    

    Reference: http://docs.djangoproject.com/en/dev/ref/contrib/admin/actions/#disabling-a-site-wide-action

    0 讨论(0)
提交回复
热议问题