Quick Contact

    Python Tutorial
    Python Panda Tutorial
    Python Selenium
    Python Flask Tutorial
    Python Django
    Interview Questions & Answers

    Django Admin Customization

    Django Admin is one of the most important tools of Django. It’s a full-fledged application with all the utilities a developer need. Django Admin’s task is to provide an interface to the admin of the web project. Django’s Docs clearly state that Django Admin is not made for frontend work.

    Django Admin’s aim is to provide a user interface for performing CRUD operations on user models and other functionalities like authentication, user access levels, etc. These all are functions that make admin more productive.

    Django Admin Customization

    We will be customizing some parts of the Django Admin. This customization will provide us with a better representation of objects. To implement it in your project, make a new app in your Django project named products.


    python manage.pystartapp products


    fromdjango.db import models
    class Product(models.Model):
    name = models.CharField(max_length = 200)
    description = models.TextField()
    mfg_date = models.DateTimeField(auto_now_add = True)
    def __str__(self):
    return self.name

    Now, we need to migrate these changes to the database. For that, execute these commands in order.

    python manage.py makemigrations

    python manage.py migrate

    We need to register these models on the Django Admin. To do this, edit the products/admin.py file. Paste this code after clearing the file.


    fromdjango.contrib import admin
    from .models import Product
    # Register your models here.
    # Ducatindia

    We will make a lot of changes to this panel.

    1. Register/ Unregister models from admin

      We can register models in admin easily. We have used the register method to register models in the admin. But, what to do for unregistering models?

      A valid point of the argument is that you simply don’t register the model. Yes, that’s true. What if we want to remove default models like Groups, Users from admin? The answer is unregister method.

      Let’s see how to unregister the model.

      Open, products/admin.py file and paste this code in it.


      fromdjango.contrib import admin
      fromdjango.contrib.auth.models import Group
      from .models import Product
      # Register your models here.
      # Ducatindia

      Now, simply reload your server and open the admin panel. You will see that the Group model is not there.

    2. Changing the title of Django Admin

      There are so many parts that can be changed from the front-end perspective. Like you can change the title written in the top-left corner of the web-page. Django administration can be changed to anything you like.

      Add this line in products/admin.py file.


      admin.site.site_header = “DucatindiaDjango Tutorials”

    3. ModelAdmin Class

      The ModelAdmin class is a representation of user-defined models in the admin panel. It can be used to override various actions. There is a whole range of options opened with ModelAdmin Class.

      We have to register the Admin Model alongside the model we want to change. Suppose, we want to exclude some fields to not be shown on the admin panel.

      We can do that by simply adding these lines in products.admin.py file.


      # ModelAdmin Class # Ducatindia
      exclude = ('description', )
    4. Customizing List Display in Django Admin

      You should add some products before we check out this one. Populate your database with 5-6 products. After you have added some products, open the products model from the admin panel. There you will see a list of Product objects. It only shows the names of the products like in the image.

      There are many scenarios where one might want more information visible in the list view. We can see all the information in the database. Similarly, we can choose the fields we want in the admin panel.


      list_display = (‘name’, ‘description’)

      The list view will now display both name & description fields.

      The list_display variable takes a list or tuple as input. You can provide the names of fields you want it to display.

    5. Adding a filter in admin

      How easy it would be to be able to filter objects. When dealing with a large database, it becomes necessary to see selected information. To perform that task easier, we can add a filter in the list view of the admin panel.


      list_filter = (‘mfg_date’, )


    Apply now for Advanced Python Training Course

    Copyright 1999- Ducat Creative, All rights reserved.

    Anda bisa mendapatkan server slot online resmi dan terpercaya tentu saja di sini. Sebagai salah satu provider yang menyediakan banyak pilihan permainan.