Skip to main content

Command Palette

Search for a command to run...

Django Admin 101: How to Add Custom Actions to the Model List View?

Easily customize the Django Admin interface with custom actions

Updated
2 min read
Django Admin 101: How to Add Custom Actions to the Model List View?

Hey everyone!

If you're a Django developer, you're probably familiar with the Django Admin interface. It's a built-in tool that allows you to manage your Django project's data through a web interface, making it easy to perform CRUD (create, read, update, delete) operations on your models.

One thing I love about Django Admin is how customizable it is. You can easily customize the look and feel of the interface, as well as the functionality.

For example, let's say you want to add a custom action to the model list view. You can do this by using the ModelAdmin.actions attribute and defining a function that takes in a request and a queryset of objects.

Here's an example of how to do this:

# myapp/admin.py

from django.contrib import admin

class MyModelAdmin(admin.ModelAdmin):
    actions = ['custom_action']

    def custom_action(self, request, queryset):
        # perform some custom action on the selected objects
        for obj in queryset:
            obj.do_something()
        self.message_user(request, "Custom action performed on {} objects.".format(len(queryset)))

# register the model with the custom action
admin.site.register(MyModel, MyModelAdmin)

And that's it! Now you have a custom action available on the model list view in Django Admin.

There are endless possibilities when it comes to customizing Django Admin, so make sure to check out the documentation for more information.

Happy coding!

Any thoughts? Write it down in the comments.

For more such crispy blogs daily, follow Dev.Junction, subscribe to our newsletter and get notified.

Django admin tips and tricks

Part 1 of 14

Django provides a great Admin panel rich in features and highly customizable, in this series of blogs we will be introduced to all such possible customizations and more.

Up next

How to register a model twice in the Django admin?

Register a model twice in the Django admin

Django Admin 101: How to Add Custom Actions to the Model List View?