gadss
gadss

Reputation: 22499

removing buttons/links in django admin

I have an apps and I am making used of django admin, but my apps does not allow deleting of data and django admin have a delete button/link. I have already removed the delete action.

my question is, how can i remove the delete button/link in admin page in django?

Upvotes: 2

Views: 3037

Answers (2)

Danny W. Adair
Danny W. Adair

Reputation: 12968

class MyAdmin(ModelAdmin):

    def get_actions(self, request):
        actions = super(MyAdmin, self).get_actions(request)
        if 'delete_selected' in actions:
            del actions['delete_selected']
        return actions

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

If the delete permission is not given the "Delete" button of a single object will disappear. The changelist "action" (select list to apply to objects with checkbox checked) will still be shown because you may have the permission to delete some of the objects. You can remove both with the above code.

CLARIFICATION (for the downvoters): admin checks if the current user has the permission to delete and then shows/hides the button/s accordingly. You wrote "my apps does not allow deleting" so I assumed you took away permissions. A superuser does not get looked up in permissions, superuser always returns "yes I have that permission". If you want the buttons to disappear for superusers, you have to use my above code. For other users just take away the permission as per Secator's answer.

Upvotes: 9

Mariusz Jamro
Mariusz Jamro

Reputation: 31663

Do not remove or change anything in the admin.

Instead remove user's/group's permission to delete given model. If user does not have the permission to delete, the delete button won't appear at any page related to that model.

Upvotes: 7

Related Questions