How to remove the default delete action in Django admin? Would the following work?
actions = [ ]
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.
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.
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.
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.
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.
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.:
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With