144
Simple
class DeleteNotAllowedModelAdmin(admin.ModelAdmin):
# Other stuff here
def has_delete_permission(self, request, obj=None):
return False
93
If you want to disable an specific one that isn’t custom do this. In django 1.6.6 I had to extend get_actions
plus define has_delete_permission
. The has_delete_permission
solution does not get rid of the action from the dropdown 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']
, only works for the custom actions (defs) you have defined for that model. The solution AdminSite.disable_action('delete_selected')
, disables it for all models, so you would have to explicitly include them later per each modelAdmin
- [Django]-Django py.test does not find settings module
- [Django]-Django get a QuerySet from array of id's in specific order
- [Django]-How to rename items in values() in Django?
11
Simply disable the yourapp.delete_yourmodel
permission for that user or the group to which (s)he belongs.
- [Django]-How to query Case-insensitive data in Django ORM?
- [Django]-Django error message "Add a related_name argument to the definition"
- [Django]-What are the differences between django-tastypie and djangorestframework?
9
Well you probably are using:
AdminSite.disable_action('delete_selected')
For further control just implement your own admin and set its actions to whatever you need:
class MyModelAdmin(admin.ModelAdmin):
actions = ['whatever', 'actions']
Reference: http://docs.djangoproject.com/en/dev/ref/contrib/admin/actions/#disabling-a-site-wide-action
- [Django]-Parsing unicode input using python json.loads
- [Django]-What is choice_set in this Django app tutorial?
- [Django]-How do I do a not equal in Django queryset filtering?
5
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):
...
- [Django]-How do I filter query objects by date range in Django?
- [Django]-How do I make many-to-many field optional in Django?
- [Django]-Python NameError: name 'include' is not defined
- [Django]-How to debug in Django, the good way?
- [Django]-Django – {% csrf_token %} was used in a template, but the context did not provide the value
- [Django]-Django: How to set a field to NULL?
0
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.
- [Django]-Pulling data to the template from an external database with django
- [Django]-How can I handle Exceptions raised by dango-social-auth?
- [Django]-How to hide some fields in django-admin?