Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove the default delete action in Django admin

How to remove the default delete action in Django admin? Would the following work?

actions = [ ]  
like image 831
Dawn T Cherian Avatar asked Dec 08 '15 09:12

Dawn T Cherian


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.

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.

What is default Django admin password?

Run 'python manage.py migrate' to apply them. Username (leave blank to use 'chatru'): admin Email address: [email protected] Password: Password (again): The password is too similar to the username.

How do you remove save and add another Django admin?

The simplest option is to set save_as=True on the ModelAdmin . This will replace the "Save and add another" button with a "Save as new" button.

How to disable delete option in Django admin?

For disabling delete option in Django admin, the first thing we need to check how does Django admin work. If you open the ModelAdmin class code you would be able to see the has_delete_permission getting called on to see if the user who is making the request has permission to delete or not.

How to remove delete_selected from the listview dropdown in Django?

By default Django adds a Delete Selected action to the listview page. You have been asked to remove the action from the Hero admin. The method ModelAdmin.get_actions returns the actions shown. By overriding this method, to remove delete_selected We can remove it form the dropdown. Your code looks like this with the changes.:

How do I change an object in Django admin?

The basic workflow of Django’s admin is, in a nutshell, “select an object, then change it.” This works well for a majority of use cases. However, if you need to make the same change to many objects at once, this workflow can be quite tedious.

How to delete students who have written exams in Django?

By adding PROTECT constraint to User model through the foreign key present in Exam model, I have disabled the power (in Django admin or elsewhere) to delete students (User) who have written exams. Show activity on this post. It removes the delete action from the list view and the delete option from the detail view.


1 Answers

This works:

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

It's also the recommended way to do this based off Django's documentation below:

Conditionally enabling or disabling actions

like image 108
Dawn T Cherian Avatar answered Sep 20 '22 05:09

Dawn T Cherian