Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove DELETE button from django admin page

I want to remove the Delete button from django admin page as shown below.

enter image description here

like image 286
vinod sagar Avatar asked Jan 26 '23 02:01

vinod sagar


2 Answers

In your ModelAdmin definition you can override the function has_delete_permission. If you wish to remove the delete button for all users you can just return False

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

I would suggest however that you allow superusers to still be able to delete

def has_delete_permission(self, request, obj=None):
    return request.user.is_superuser
like image 50
Iain Shelvington Avatar answered Jan 29 '23 14:01

Iain Shelvington


You can override the function has_delete_permission to prevent deletion

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

Overriding has_delete_permission will prevent deletion in other model pages which have relation (with on_delete=models.CASCADE) to your model. To allow deletion in other pages but prevent the deletion in the model's admin page, You can conditionally return False like:

def has_delete_permission(self, request, obj=None):
    if f"{model._meta.app_label}/{model._meta.model_name}" in request.path:
        return False
    return True
like image 25
iamwaseem99 Avatar answered Jan 29 '23 14:01

iamwaseem99