Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The default "delete selected" admin action in Django

How can I remove or change the verbose name of the default admin action "delete selected X item" in the Django admin panel?

like image 595
Hellnar Avatar asked Oct 14 '09 11:10

Hellnar


People also ask

What is default Django admin password?

In the example, we have successfully reset the password for username “admin“.

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 does admin do in Django?

The Django admin application can use your models to automatically build a site area that you can use to create, view, update, and delete records. This can save you a lot of time during development, making it very easy to test your models and get a feel for whether you have the right data.

How do I restrict admin 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 .


2 Answers

Alternatively to Googol's solution, and by waiting for delete_model() to be implemented in current Django version , I suggest the following code.

It disables the default delete action for current AdminForm only.

class FlowAdmin(admin.ModelAdmin):
    actions = ['delete_model']

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

    def delete_model(self, request, obj):
        for o in obj.all():
            o.delete()
    delete_model.short_description = 'Delete flow'

admin.site.register(Flow, FlowAdmin)
like image 116
Stéphane Avatar answered Sep 19 '22 14:09

Stéphane


You can disable the action from appearing with this code.

from django.contrib import admin
admin.site.disable_action('delete_selected')

If you chose, you could then restore it on individual models with this:

class FooAdmin(admin.ModelAdmin):
    actions = ['my_action', 'my_other_action', admin.actions.delete_selected]
like image 40
Joe Germuska Avatar answered Sep 16 '22 14:09

Joe Germuska