In Django Admin how do I disable the Delete link

Viewed 39289

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 Answers

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):
    ...

This is very old, but still, it may help someone.

Assuming that OP's

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

refers to the red button in the "change" view. This button can be removed by extending the ModelAdmin.change_view method as follows:

def change_view(self, request, object_id=None, form_url='', extra_context=None):
    return super().change_view(request, object_id, form_url,
                               extra_context=dict(show_delete=False))

You can do the same with show_save, and show_save_and_continue. More info and alternatives here.

Also note that, as of version 2.1, Django has a separate has_view_permission (docs), which may be a better option, depending on your use case.

Related