How to Create Custom Admin List Actions in Django?

Published On: 15/11/2022 | Category: Django

Hi Dev,

In this example, I will show you how to create custom django admin list actions. this example will help you custom actions in django admin. you can see how to add custom django admin list actions. I’m going to show you about django custom admin list actions example. Here, Creating a basic example of custom actions in django admin example tutorial.

Each of the listed actions is a standard Python function that has three inputs: the current ModelAdmin, a HttpRequest object (similar to a view function), and a QuerySet, which is a list of chosen model instances.

These Action Functions can be located in your app's module. However, if they begin to grow significantly, you can define them outside of

Here i explained simply step by step example of how to customize django admin site.

Step 1: Create a Project

In this step, we’ll create a new django project using the django-admin. Head back to your command-line interface and run the following command:

django-admin startproject example

Step 2: Create a App

Now we'll create a single app called core to store a list of post names. We're keeping things intentionally basic. Stop the local server with Control+c and use the startapp command to create this new app.

python startapp core

Step 3: Update

In this step we require to do two things in our file, One is our installed app name Add the below lines to your file:

Next, you need to add it in the file as follows:


Step 4: Create a Model

In this step we will require the database model for storing contacts.Open the core/ file and add the following code:

from django.db import models

class Book(models.Model):
    EBOOK = 3
    BOOK_TYPES = (
        (HARDCOVER, 'Hardcover'),
        (PAPERBACK, 'Paperback'),
        (EBOOK, 'E-book'),
    title = models.CharField(max_length=50)
    publication_date = models.DateField(null=True)
    author = models.CharField(max_length=30, blank=True)
    price = models.DecimalField(max_digits=5, decimal_places=2)
    pages = models.IntegerField(blank=True, null=True)
    book_type = models.PositiveSmallIntegerField(choices=BOOK_TYPES)

    class Meta:
        verbose_name = 'book'
        verbose_name_plural = 'books'

Ok, all set. We can engender a migrations file for this change, then integrate it to our database via migrate.

python makemigrations
python migrate

Step 5: Update

from django.db import models
from .models import Book
import decimal

def apply_discount(modeladmin, request, queryset):
    for book in queryset:
        book.price = book.price * decimal.Decimal('0.9')
apply_discount.short_description = 'Apply 10%% discount'

class BookAdmin(admin.ModelAdmin):
    list_display = ['title', 'publication_date', 'author', 'price', 'book_type']
    actions = [apply_discount, ], BookAdmin)
Run the Server

In this step, we’ll run the local development server for playing with our app without deploying it to the web.

python runserver

Next, go to the http://localhost:8000/admin address with a web browser.

I Hope It will help you....