Innovate anywhere, anytime withruncode.io Your cloud-based dev studio.
Django

How to Customize the Admin Actions in List Pages of Django Admin?

2022-07-20

Django by default provides automatic admin interface, that reads metadata from your models to provide a beautiful model interface where trusted users can manage content on your site. The admin is enabled in the default project template used by startproject so we don't need to worry of the settings.

By default the model won't get displayed in the admin panel, to make the models of our application visible in the admin panel we have to regsiter the models in admin.py with admin.

In models.py

STATUS_CHOICES = (
    ('d', 'Draft'),
    ('p', 'Published'),
    ('r', 'Review'),
    ('t', 'Trash'),
)

class BlogPost(models.Model):
    title = models.CharField(max_length=100)
    content = models.TextField()
    status = models.CharField(max_length=1, choices=STATUS_CHOICES)
    created_date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title

In admin.py

admin.site.register(BlogPost)

Customizing the admin actions:

When we register our app with the admin we’ll see the table in the admin panel. By default it will come with ‘delete selected’ action in the list page.
Now we want to add some actions like Publish the selected posts or Draft the selected posts. then we would be override the ModelAdmin and write our custom action in admin.py


In admin.py

class BlogPostAdmin(admin.ModelAdmin):

    actions = ['publish_selected']

    def publish_selected(self, request, queryset):
        queryset.update(status='p')

    publish_selected.short_description = "Publish the selected posts"

In the above snippet actions atribute of  BlogPostAdmin adds the new action ‘‘publish_selected'’ to the actions dropdown. And the coresponding business logic will be written in the mothod ‘‘publish_selected'’. The short_description that is given will be displayed in the dropdown, in the current case it is Publish the selected posts.
In the above scenario the action will be added only to the BlogPost table only in the admin panel, to make the above action to be added for all other tables in the admin panel we need to write the method globally and add that to add_action.

In admin.py

def publish_selected(modeladmin, request, queryset):
    queryset.update(status='p')

publish_selected.short_description = "Publish the selected posts"

admin.site.add_action(publish_selected')

You can disable any action globally by using disable_action method of django admin.

admin.site.disable_action('delete_selected')

The above line would disable 'delete_selected' for all the models. If you want to enable the action for single model and disable for all other models then in admin.py

admin.site.disable_action('delete_selected')

class BlogPostAdmin(admin.ModelAdmin):

    actions = ['delete_selected', 'publish_selected']

    def publish_selected(self, request, queryset):
        queryset.update(status='p')

    publish_selected.short_description = "Publish the selected posts"

The above line will add ‘delete_selected’ only for ‘BlogPostAdmin’ model.

If you want to remove ‘delete_selected’ for only Single model then we have to override the get_actions method of Django model admin and delete the corresponding ‘action’.

In admin.py

class BlogPostAdmin(admin.ModelAdmin):

    actions = ['delete_selected', 'publish_selected']

    def publish_selected(self, request, queryset):
        queryset.update(status='p')

    publish_selected.short_description = "Publish the selected posts"

    def get_actions(self, request):
        # Disable delete
        actions = super(BlogPostAdmin, self).get_actions(request)
        del actions['delete_selected']
        return actions

The above will disable the ‘delete_selected’ only for BlogPostAdmin model.