Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Django Admin how do I disable the Delete link

I've managed to disable the "Delete selected" action. Easy.

But a user can still click on an item and then there's the red Delete link at the bottom.

like image 221
Peter Bengtsson Avatar asked Oct 28 '10 14:10

Peter Bengtsson


People also ask

How can I remove extra's from Django admin panel?

Take a look at the Model Meta in the django documentation. Within a Model you can add class Meta this allows additional options for your model which handles things like singular and plural naming. Show activity on this post. inside model.py or inside your customized model file add class meta within a Model Class.

How do I restrict access to admin pages in Django?

Django admin allows access to users marked as is_staff=True . To disable a user from being able to access the admin, you should set is_staff=False . This holds true even if the user is a superuser. is_superuser=True .

What is Admin ModelAdmin in Django?

One of the most powerful parts of Django is the automatic admin interface. It reads metadata from your models to provide a quick, model-centric interface where trusted users can manage content on your site. The admin's recommended use is limited to an organization's internal management tool.


2 Answers

Simple :)

class DeleteNotAllowedModelAdmin(admin.ModelAdmin):     # Other stuff here     def has_delete_permission(self, request, obj=None):         return False 
like image 55
Jonathan R. Avatar answered Oct 03 '22 21:10

Jonathan R.


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

like image 28
radtek Avatar answered Oct 03 '22 20:10

radtek