How to customize the admin actions in list pages of Django admin?

Reading Time : ~ .

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.

    By Posted On
SENIOR DEVELOPER at MICROPYRAMID

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
Implement search with Django-haystack and Elasticsearch Part-I Ravi Kumar Gadila

Haystack works as search plugin for django. You can use different back ends Elastic-search, Whose, Sorl, Xapian to search objects. All backends work with same ...

Continue Reading...
How to Create initial django migrations for existing DB schema. Chaitanya Kattineni

Django provides the comfort database migrations from its version 1.8, with which we can avoid the usage of third party packages like south. Adding migrations ...

Continue Reading...
Django Conditional Expressions in Queries Anjaneyulu Batta

Reduce database queries in django with Conditional Expressions. By using Conditional Expressions we can use "If...Elif...Else" expressions while querying the database so that we can ...

Continue Reading...

Subscribe To our news letter

Subscribe to our news letter to receive latest blog posts into your inbox. Please fill your email address in the below form.
*We don't provide your email contact details to any third parties