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.

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
Latest Comments
Related Articles
Creating Django App Siva Chittamuru

Django is a high-level, free and open-source Python Web framework that encourages rapid development. Django follows the model–view–controller (MVC) architectural pattern. Django's primary goal is ...

Continue Reading...
Django Raw Sql Queries Vamsi Popuri

When your model query API don't go well or you want more performance, you can use raw sql queries in django. The Django Object Relational ...

Continue Reading...
Django Unit Test cases with Forms and Views Swetha Naretla

By writing unit test cases, you can evaluate each code component in the initial stage itself and it'll improve your app/code performance. Which is the ...

Continue Reading...