DEV Community

Heinek Halwin
Heinek Halwin

Posted on

Customising the Admin Page in Django

Hi all,

Is it customising or customizing ?? Well, thats not the point here. Have you ever wondered why your models in django admin page has only one column while the default user model has more than one ? Well, fear not. In this blog, i will show you how to customise your admin page to show more than one column.

Well, firstly you need to create to model to register in your django admin page. Go over to your models.py file in your app and create your model.

from django.db import models


class Student(models.Model):
    name = models.CharField(max_length=30)
    age = models.IntegerField()
    nationality = models.CharField(max_length=30)

    def __str__(self):
        return self.name

Now, i acknowledge that the database is awful and its only for the sake of this blog. Now, we can go ahead and register this model in our admin.py file, so that we can view this in our admin page.

from django.contrib import admin
from .models import Student


admin.site.register(Student)

Now,if you looked in your admin page, your model would still be in a single column displaying only the names of the students.

Customising the admin page

Listing more values

For customising the admin page, all you need to do is modify the admin.py file.

For adding more columns and viewing the other values in the student model, all you need to do is.

from django.contrib import admin
from .models import Student


class StudentAdmin(admin.ModelAdmin):        
    list_display = ('id','name','age','nationality') #for displaying more values as columns

admin.site.register(Student, StudentAdmin)

Filtering from database values

Suppose, you have a lot of values and you want to filter the values based on some attribute from your model, you can do that too. In my case, i want to filter my values based on the age and nationality.

from django.contrib import admin
from .models import Student


class StudentAdmin(admin.ModelAdmin):        
    list_display = ('id','name','age','nationality') #for displaying more values as columns
    list_filter = ('age','nationality') #for filtering

admin.site.register(Student, StudentAdmin)

Now, you can filter your values. What if you wanted a customer filter, you can simply create your own filter as a function and add it to your list_filter tuple.

Performing custom action

One last customisation is the ability to perform a certain action from the dropdown in your admin page. All you need to do is write a function, which you can add to your ModelAdmin class.

from django.contrib import admin
from .models import Student


def student_function():
    do_something


class StudentAdmin(admin.ModelAdmin):        
    list_display = ('id','name','age','nationality') #for displaying more values as columns
    list_filter = ('age','nationality') #for filtering
    actions = [student_function] #custom actions

admin.site.register(Student, StudentAdmin)

Thats all ! Now, you have a custom action, column-wise view of your database values, filtering your database in your admin page.

Have a great day !

Top comments (0)