Customize Django Admin Interface

Django Admin Interface is a powerful and responsive GUI, which provides many features including CRUD for the ease of developers as well as users, as a developer you have used it many times but sometimes it needs customization right? So Django provides you the capability to even customize the Django admin interface isn’t it awesome you can make changes in it accordingly. So in this blog we are going to learn some customizations.

Customize Django Admin Interface

Implementation

1. Creating Django Project

  1. We have to create a django project in which we want to work on, so for that you have to write the following command in your terminal.
django-admin startproject blog17

Note:- Here, blog17 is the name of my project. You can name it according to your choice.

2. Now, create an app in your django project with the help of the following command.

python manage.py startapp customizeadmin

Note:- Here, cuatomizeadmin is the name of my application/ app you can name it according to your choice.

3. Add your app name in installed apps.

Settings.py

INSTALLED_APPS = [
    'customizeadmin',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

2. Creating Model

We need to create a model that we want to customize in our admin panel. Here,in this section  we are creating a simple student model and registering it.

  1. Create a model in the app folder here customizeadmin > models.py.

Models.py

from django.db import models
class Student(models.Model):
    name=models.CharField(max_length=50)
    age=models.IntegerField()
    address=models.CharField(max_length=255)

2. Make migrations by running the below commands in your terminal.

python manage.py makemigrations
python manage.py sqlmigrate customizeadmin 0001

Note:- Here you have to add your app name instead of customize admin (as my app name is customizeadmin).

python manage.py migrate

3. Register the model in admin.py here customizeadmin > admin.py.

Admin.py

from customizeadmin.models import Student
from django.contrib import admin
from .models import Student
admin.site.register(Student)

4. Create Super User, run the given command in your terminal and add the credentials.

python manage.py createsuperuser

Output :-

Customize Django Admin Interface

3. Customizing Django Admin

Now we are going to customize our django admin panel for the model that we had created in the previous step. So we will learn about the following customizations:-

  • Changing model name.
  • Showing multiple fields in listing.
  • Adding multiple actions.
  • Removing delete action.
  • Removing add option.

Let’s go through the customization

1. Changing model name

In this section we will learn how to change the default name of our model that appears in django admin panel, Suppose you create a model name as employee the django admin panel by default shows that name but you want to show different name for it let say you want to show EMPLOYEE_LIST instead of employee, so how to do it? Well it is pretty simple just add verbose_name in the meta class of your model.

Models.py

from django.db import models
class Student(models.Model):
    name=models.CharField(max_length=50)
    age=models.IntegerField()
    address=models.CharField(max_length=255)
 
    class Meta:
        verbose_name="Student Created"

So now instead of Student we will get student created as our model name.

Output :-

changing name
Before Changing Name
changing name
After Changing Name
Customize Django Admin Interface
2. Showing multiple fields in listing

In this section we will learn how to show the other fields of our model instead of by default object name.

In your admin panel just add the following code and keep the model the same as it is.

Admin.py

from customizeadmin.models import Student
from django.contrib import admin
from .models import Student
class StudentAdmin(admin.ModelAdmin):
    list_display = ('name', 'age', 'address')
admin.site.register(Student,StudentAdmin)

list_display contains the list of fields that you want to display.

Output :-

multiple fields list
3. Adding Multiple Actions

In this section we will learn how to add multiple actions, if you notice then there is only one default delete action provided by django admin but we can customize it, we can add other actions as well.

First let us add another field in our model which is to mark present or absent.

Models.py

from django.db import models
class Student(models.Model):
    name=models.CharField(max_length=50)
    age=models.IntegerField()
    address=models.CharField(max_length=255)
    is_present = models.IntegerField(default = 1,blank = True, null = True, help_text ='1->Present, 0->Absent',  choices =((1, 'Present'), (0, 'Absent')))
 
    class Meta:
        verbose_name="Student Created"

Now add the given code in admin file

Admin.py

from customizeadmin.models import Student
from django.contrib import admin
from .models import Student
from django.contrib import messages 
class StudentAdmin(admin.ModelAdmin):
    list_display = ('name', 'age', 'address','is_present')
  
    def active(self, obj):
        return obj.is_present == 1
  
    active.boolean = True
  
    def make_active(modeladmin, request, queryset):
        queryset.update(is_active = 1)
        messages.success(request, "Selected Record(s) Marked as Present Successfully !!")
  
    def make_inactive(modeladmin, request, queryset):
        queryset.update(is_active = 0)
        messages.success(request, "Selected Record(s) Marked as Absent Successfully !!")
  
    admin.site.add_action(make_active, "Mark Present")
    admin.site.add_action(make_inactive, "Mark Absent")
    
 
admin.site.register(Student,StudentAdmin)

Output :-

adding actions
4. Removing Delete Action

In this section we will learn how to remove the default delete action.

Admin.py

from customizeadmin.models import Student
from django.contrib import admin
from .models import Student
from django.contrib import messages 
class StudentAdmin(admin.ModelAdmin):
    list_display = ('name', 'age', 'address','is_present')
  
    def active(self, obj):
        return obj.is_present == 1
  
    active.boolean = True
  
    def make_active(modeladmin, request, queryset):
        queryset.update(is_active = 1)
        messages.success(request, "Selected Record(s) Marked as Present Successfully !!")
  
    def make_inactive(modeladmin, request, queryset):
        queryset.update(is_active = 0)
        messages.success(request, "Selected Record(s) Marked as Absent Successfully !!")
  
    admin.site.add_action(make_active, "Mark Present")
    admin.site.add_action(make_inactive, "Mark Absent")
    def has_delete_permission(self, request, obj = None):
        return False
 
admin.site.register(Student,StudentAdmin)

Output :-

removing delete action
5. Removing Add Button

In this section we will learn how to remove the default add button.

Admin.py

from customizeadmin.models import Student
from django.contrib import admin
from .models import Student
from django.contrib import messages 
class StudentAdmin(admin.ModelAdmin):
    list_display = ('name', 'age', 'address','is_present')
  
    def active(self, obj):
        return obj.is_present == 1
  
    active.boolean = True
  
    def make_active(modeladmin, request, queryset):
        queryset.update(is_active = 1)
        messages.success(request, "Selected Record(s) Marked as Present Successfully !!")
  
    def make_inactive(modeladmin, request, queryset):
        queryset.update(is_active = 0)
        messages.success(request, "Selected Record(s) Marked as Absent Successfully !!")
  
    admin.site.add_action(make_active, "Mark Present")
    admin.site.add_action(make_inactive, "Mark Absent")
    def has_add_permission(self, request, obj = None):
        return False
 
admin.site.register(Student,StudentAdmin)

Output :-

Before removing add button
Before removing add button
After removing add button
After removing add button
removing add action

Conclusion

So, we learned how to Customize Django Admin Interface. I hope this blog will be useful for you and helps you to understand each and every step. Hope all your doubts get cleared. Thank you.