By continuing to navigate on this website, you accept the use of cookies to serve you more relevant services & content.
For more information and to change the setting of cookies on your computer, please read our Cookie Policy.

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

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.

    Posted On
  • 30 April 2014
  • By
  • Micropyramid

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
Django Generic many to many field implementation

Django application has a GM2MField that combines the features of the standard Django "ManyToManyField" and "GenericForeignKey".

Installation:
pip install django-gm2m

Continue Reading...
Improving page speed score in Google Page Score test- PART2

In our previous blog post we got an idea of what are the things that Google's Page Speed Insights will take into count to give ...

Continue Reading...
Introduction to API development using Django REST framework with Example

Introduction to API development with Django REST framework. You can build the API for any Django application. Pre-requisites are Django and OOPS(object oriented programming concepts) ...

Continue Reading...
open source packages

Subscribe To our news letter

Subscribe and Stay Updated about our Webinars, news and articles on Django, Python, Machine Learning, Amazon Web Services, DevOps, Salesforce, ReactJS, AngularJS, React Native.
* We don't provide your email contact details to any third parties