Remove the default delete action in Django admin
In your admin class, define has_delete_permission
to return False
:
class YourModelAdmin(admin.ModelAdmin):
...
def has_delete_permission(self, request, obj=None):
return False
Then, it will not show delete button, and will not allow you to delete objects in admin interface.
You can disable "delete selected" action site-wide:
from django.contrib.admin import site
site.disable_action('delete_selected')
When you need to include this action, add 'delete_selected'
to the action list:
actions = ['delete_selected']
Documentation
This works:
def get_actions(self, request):
actions = super().get_actions(request)
if 'delete_selected' in actions:
del actions['delete_selected']
return actions
It's also the recommended way to do this based off Django's documentation below:
Conditionally enabling or disabling actions