Customize Django Admin Interface - GeeksforGeeks

06 Mar, 2020
Django admin by default is highly responsive GUI, which offers various features and an overall CRUD application to help developers and users. Moreover, Django admin can be customized to fulfill one’s needs such as showing fields on the home page of the table, etc. In this article, we will discuss how to enhance Django-admin Interface.
Project structure looks like:
Let us create an app called state which has one model with the same name(state). When we register app to admin.py it shows like.
from django.db import modelsfrom django.utils import timezone  class State(models.Model):    name = models.CharField(max_length = 50)    is_active = models.IntegerField(default = 1,                                   blank = True,                                    null = True,                                    help_text ='1->Active, 0->Inactive',                                     choices =(                                    (1, 'Active'), (0, 'Inactive')                                    ))    created_on = models.DateTimeField(default = timezone.now)    updated_on = models.DateTimeField(default = timezone.now,                                    null = True,                                     blank = True                                    )    def __str__(self):        return self.name      class Meta:        db_table = 'state'
state/admin.py:
from django.contrib import adminfrom .models import State  admin.site.register(State)
Check it in the django admin interfaceNow lets’ customize django admin according to available options.
1. Change model name:If you want to change name of model which is States here so open model.py file and add verbose_name attribute in meta section.
state/model.py
from django.db import modelsfrom django.utils import timezone  class State(models.Model):    name = models.CharField(max_length = 50)    is_active = models.IntegerField(default = 1,                                   blank = True,                                    null = True,                                    help_text ='1->Active, 0->Inactive',                                     choices =(                                    (1, 'Active'), (0, 'Inactive')                                    ))    created_on = models.DateTimeField(default = timezone.now)    updated_on = models.DateTimeField(default = timezone.now,                                    null = True,                                     blank = True                                    )    def __str__(self):        return self.name    class Meta:        db_table = 'state'        # Add verbose name        verbose_name = 'State List'
Output :
2. By default django admin shows only object name in listing.
One can show multiple fields data from model. Add some lines of code in your admin.py file.
state/admin.py:
from django.contrib import adminfrom .models import State  class StateAdmin(admin.ModelAdmin):    list_display = ('name', 'active', 'created_on')      def active(self, obj):        return obj.is_active == 1      active.boolean = True  admin.site.register(State, StateAdmin)
Output :
3. By default there is only one option which is delete option.One can add more option on Action dropdown:
state/admin.py:
from django.contrib import adminfrom django.contrib import messagesfrom .models import State  class StateAdmin(admin.ModelAdmin):    list_display = ('name', 'active', 'created_on')      def active(self, obj):        return obj.is_active == 1      active.boolean = True      def make_active(modeladmin, request, queryset):        queryset.update(is_active = 1)        messages.success(request, "Selected Record(s) Marked as Active Successfully !!")      def make_inactive(modeladmin, request, queryset):        queryset.update(is_active = 0)        messages.success(request, "Selected Record(s) Marked as Inactive Successfully !!")      admin.site.add_action(make_active, "Make Active")    admin.site.add_action(make_inactive, "Make Inactive")  admin.site.register(State, StateAdmin)
Output:
4. Disable Delete option:
state/admin.py:
from django.contrib import adminfrom django.contrib import messagesfrom .models import State  class StateAdmin(admin.ModelAdmin):    list_display = ('name', 'active', 'created_on')      def active(self, obj):        return obj.is_active == 1      active.boolean = True      def make_active(modeladmin, request, queryset):        queryset.update(is_active = 1)        messages.success(request, "Selected Record(s) Marked as Active Successfully !!")      def make_inactive(modeladmin, request, queryset):        queryset.update(is_active = 0)        messages.success(request, "Selected Record(s) Marked as Inactive Successfully !!")      admin.site.add_action(make_active, "Make Active")    admin.site.add_action(make_inactive, "Make Inactive")      def has_delete_permission(self, request, obj = None):        return False  admin.site.register(State, StateAdmin)
Output:
5. Remove Add option:
state/admin.py:
from django.contrib import adminfrom .models import State  class StateAdmin(admin.ModelAdmin):    list_display = ('name', 'active', 'created_on')      def active(self, obj):        return obj.is_active == 1      active.boolean = True      def has_add_permission(self, request):        return False  admin.site.register(State, StateAdmin)
Python Django
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Read a file line by line in Python
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
Iterate over a list in Python
Python String | replace()
*args and **kwargs in Python
Reading and Writing to text files in Python
Create a Pandas DataFrame from Lists