In Django Admin, how to disable the Delete link

I managed to disable the "Delete Selected" action. Easily.

But the user can still click on the item, and then a red Delete link will appear at the bottom.

+56
django django-admin
Oct 28 2018-10-28
source share
6 answers

Plain:)

class DeleteNotAllowedModelAdmin(admin.ModelAdmin): # Other stuff here def has_delete_permission(self, request, obj=None): return False 
+104
Aug 11 '11 at 18:39
source share

If you want to disable a specific one that is not ordinary, do it. In django 1.6.6, I had to stretch get_actions plus define has_delete_permission . The has_delete_permission solution has_delete_permission not eliminate the action from the drop-down list 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'] , works only for user actions (defs) defined for this model. The AdminSite.disable_action('delete_selected') solution disables it for all models, so you will have to explicitly enable them later for each model. Admin

+70
Sep 12 '14 at 4:55
source share

Just disable the permission yourapp.delete_yourmodel for this user or group to which he belongs.

+11
Oct 28 '10 at 17:33
source share

Well, you are probably using:

 AdminSite.disable_action('delete_selected') 

For further management, simply run your own administrator and set its actions to everything you need:

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

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

+9
Oct 28 '10 at 14:30
source share

admin.site.disable_action('delete_selected')

From docs

+1
Apr 27 '17 at 13:20
source share

It is very old, but, nevertheless, it can help someone.

Assuming that OP

... the user can still click on the item, and then a red Delete link will appear at the bottom.

refers to the red button in the edit view. You can remove this button 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 Django 2.1 now has a separate has_view_permission ( docs ), which may be a better option , depending on your use case.

0
Jan 17 '19 at 13:55
source share



All Articles