How To Add Models To Django Admin Site

When you create a Django project and start the project built-in web server successfully, it also contains an admin website in this project. This admin website is the result of django.contrib.admin and django.contrib.auth Django applications. The Django project installed some default applications in the project settings.py file when you create it. You can find them in the INSTALLED_APPS section in settings.py file like below. And this is why you should run command python manage.py makemigrations and python manage.py migrate to create those applications models mapped tables in the backend database.

django project app dept_emp source files structure

# Application definition

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

1. Login To Django Project Admin WebSite To Manage Model Objects.

  1. The Django project admin website provide a lot of functions, and the most important one is the ability of manage model objects ( add, delete, search and update ), these actions will also operate data record in backend database tables.
  2. To manage model objects, login to the admin site with url http://127.0.0.1:8000/admin/, and provide correct admin username and password( please refer article How To Create / Remove Django Project Application).
  3. Then you will see a page that list all installed applications and models in it like below. From below picture, we can see that each table title is the application name, and the table content is the models used in those applications. The Groups and Users are models used by the default added django.contrib.admin and django.contrib.auth applications. The Departments and Employees are models used in my own Django application USER_REGISTER_LOGIN. When you click the models link, then you can operate model ( add, delete, update, search ) in the model list page.
    django app models table list page
READ :   Django Custom Login Page Use login_required Decorator

2. Add Custom Django App Models In Admin Site.

By default, there will not show custom app models in above list, if you want to manage your custom app models object in the admin site, follow below steps.

  1. Register your Django application in project settings.py file (DjangoHelloWorld / DjangoHelloWorld / settings.py ) INSTALLED_APPS section. This example installed bootstrap3 and dept_emp applications in it. bootstrap3 app is a third party library which integrate bootstrap in the application, you can read article Django Bootstrap3 Example.
    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'bootstrap3',
        'dept_emp',
    ]
  2. Then define model class in app_name (dept_emp) / models.py file like below.
    from django.db import models
    
    class Department(models.Model):
        # define department name and description columns, the id column will be added automatically.
        dept_name = models.CharField(max_length=1000)
        dept_desc = models.CharField(max_length=1000)
    
        # this is a inner class which is used to define unique index columns. You can specify multiple columns in a list or tuple.
        class Meta:
            unique_together = ['dept_name']
  3. Register above model class with admin.site.register function in app_name (dept_emp) / admin.py file.
    from django.contrib import admin
    
    # the module name is app_name.models
    from dept_emp.models import Department
    
    # Register your models to admin site, then you can add, edit, delete and search your models in Django admin site.
    admin.site.register(Department)
  4. Then go to admin website, and click Departments link, now you will get below list page, the department name column is not user friendly.
    the department list page department name value is not user friendly

3. How To Make Custom Model Object Name User Friendly.

To resolve the model object name not user friendly issue, you can change the model class definition in dept_emp / models.py file. Add  __str__(self) function to each model class definition.

from django.db import models

class Department(models.Model):
    # define department name and description columns, the id column will be added automatically.
    dept_name = models.CharField(max_length=1000)
    dept_desc = models.CharField(max_length=1000)

    # this function will be invoked when this model object is foreign key of other model(for example Employee model.).
    def __str__(self):
        ret = self.dept_name + ',' + self.dept_desc
        return ret

    # this is a inner class which is used to define unique index columns. You can specify multiple columns in a list or tuple.
    class Meta:
        unique_together = ['dept_name']

Save the file and refresh the Departments list page again, now each department column name will contains department_name and department_desc value.

READ :   Django User Registration And Login Use Built-in Authorization Example

customize model list column value

2 Comments


  1. my model.py class name Contact not showing in admin page.help

    Reply

  2. Hey hi, How to write the unit test cases for Add/delete/edit Models To Django Admin Site?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.