How to Create Custom Admin List Actions in Django?
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 admin.py module. However, if they begin to grow significantly, you can define them outside of admin.py.
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 manage.py startapp core
Step 3: Update setting.py
In this step we require to do two things in our settings.py file, One is our installed app name Add the below lines to your settings.py file:
Next, you need to add it in the settings.py file as follows:example/settings.py
.... INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'core', ]
Step 4: Create a Model
In this step we will require the database model for storing contacts.Open the core/models.py file and add the following code:core/models.py
from django.db import models class Book(models.Model): HARDCOVER = 1 PAPERBACK = 2 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 manage.py makemigrations python manage.py migrate
Step 5: Update admin.pycore/admin.py
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') book.save() apply_discount.short_description = 'Apply 10%% discount' class BookAdmin(admin.ModelAdmin): list_display = ['title', 'publication_date', 'author', 'price', 'book_type'] actions = [apply_discount, ] admin.site.register(Book, 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 manage.py runserver
Next, go to the http://localhost:8000/admin address with a web browser.
I Hope It will help you....