• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

How to Customize Django Admin Dashboard Site Panel?

Aniruddha Chaudhari/2673/0
Django

Do you want to customize your Django Admin site so that it will be easier for you to track all the users and model data on the Django admin interface?

In an earlier tutorial, we have seen how to add a model to the Django admin site. I will be sharing some important tricks and tips to help you out customize your Django admin site based on your requirements.

Before we start to customize Django admin dashboard, let’s consider we have the Django model.

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    mobile = models.IntegerField(blank=True, null=True)
    city = models.CharField(max_length=200, default="", null=True)
    country = models.CharField(max_length=200, default="", null=True)

Here, “user” is the key inherited from the actual Django “User” model using the OneToOneField model field.

Django User model already has 12 model fields. Apart from that if you want to add any new field to the User model, it is better to create a new model (say ‘Profile’).

First of all, create admin.py file in your working directory (in the same directory where models.py is present) if it is not present.

You can log in to your admin panel by hitting the admin URL.

http://{url}/admin

If you are running your Django site locally, here is the URL.

http://127.0.0.1:8000/admin

Now you can see the default Django admin dashboard but you do not see any model data as we have not registered them to our admin interface.

Let’s begin.

1. How to show all the fields from model?

You have to register for the class and make a list of all the fields (list_display) that you want to show in the admin dashboard. Here is the sample code.

from django.contrib import admin
from .models import ProfileModel

@admin.register(ProfileModel)
class ProfileAdmin(admin.ModelAdmin):
  list_display = [field.name for field in ProfileModel._meta.get_fields()]

Here, we are using the list comprehension technique to get the list of all the fields from the profile model and assign them to the list “list_display”.

Save the file and run the Django server. If you open the admin interface, you will see the “ProfileModel” where you can see all the profiles. You can also delete and edit the profile entries.

Similarly, you have to write the class for all the models to show them in the admin site. It’s pretty easy, isn’t it?

Let’s see some of the very important and required customization for Django admin interface.

2. How to add field from another model?

Let’s say you want to add the email ID in the user profile from the Django User modal. Here is the sample code.

from django.contrib import admin
from .models import ProfileModel

@admin.register(ProfileModel)
class ProfileAdmin(admin.ModelAdmin):
  list_display = [field.name for field in ProfileModel._meta.get_fields()]
  list_display.append('email')

  def email(self, obj):
    return obj.user.email

It will append the field “email” to the “ProfileModel” in your Django admin.

Note: The name of the function and field should be the same.

I will keep adding more ways to customize the Django admin dashboard site. Let me know if you have specific requirements to customize the Django admin interface. I would like to help you out.

Python Interview Questions eBook

Django
Aniruddha Chaudhari
I am complete Python Nut, love Linux and vim as an editor. I hold a Master of Computer Science from NIT Trichy. I dabble in C/C++, Java too. I keep sharing my coding knowledge and my own experience on CSEstack.org portal.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Leave a Reply Cancel reply

Prerequisite for Django

Learn Python Basics

Django Tutorials

Why Django?

Django- Create Your First Project

Django- Adding CSS/Static Files

Django- Use/Load Static Files

Django- Create Form

Django- Create Input-Tags

Django- Display Message on Form Submit

Django- MultipleChoiceField in Form

Django- Read Model Data (order_by)

Django- Read First/Last Queryset Object

Django- Update Model Field

Django- Signals

Django- Create TextField in Form

Django- Add Model to Admin Site

Django- Customize Admin Site

Django- Understanding Model User

Django- Registration/Signup Form

Django- Form Layout with Widget-Tweaks

Django- OneToOneField in Model

Django- Foreign Key & Reverse Lookup

Django- Custom Template Filters

Django- Interactive Shell

Django- Social Login

Django- Deploy and Host [FREE] 🔥

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard