Django Admin Custom Action

By default, Django admin provide delete model in action. We can add custom action.

Here some quick steps to add custom action.

Step 1: Preparation
a. Create virtualenv and start it.

> virtualenv venv
> venv\Scripts\activate

b. Install Django

(venv)> pip install django

c. Create Django Project

(venv)> django-admin startproject myproject

d. Goto Project Folder and Create Django Application

(venv)> cd myproject
(venv)/ myproject> python manage.py startapp myapp

e. Register Django App to Django Project
add ‘myapp’ in INSTALLED_APPS in myproject/settings.py

f. Initial Migrate

(venv)/ myproject> python manage.py migrate

g. Create superuser

(venv)/ myproject> python manage.py createsuperuser

type username, email, password and retype password.

Step 2: Create Book Model

a. Create Book Model
add book model in myapp/models.py

b. Register model and add custom action
register book model in myapp/admin.py

action above will change selected book to ‘Out of Stock’.

c. Make migration and migrate

(venv)/ myproject> python manage.py makemigrations
(venv)/ myproject> python manage.py migrate

Step 3: Start Apps

(venv)/ myproject> python manage.py runserver

open http://127.0.0.1:8000/admin open Book menu and add some book data and do some test with new custom action.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store