Table of contents
1.
Introduction
2.
Django Admin Interface
3.
Rendering Django Model in the Admin Interface
3.1.
Setting up the Django Project
3.2.
Designing the Django Model
3.3.
Setting up Migrations
3.4.
Running the Migrate
3.5.
Entering the Data in Shell
3.6.
Running the Server & Django Admin Interface
4.
Frequently Asked Questions
5.
Key Takeaways
Last Updated: Mar 27, 2024

Rendering Django Model in the Admin Interface

Author Sneha Mallik
2 upvotes
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The term "rendering Django model in the admin" refers to the process of introducing a model to the admin interface so that the data can be easily changed. Django's ORM(Object Relational Mapper) provides a standardized admin interface that uses basic database operations like INSERT, SELECT, CREATE and SEARCH. To start placing data into your model and utilizing the admin interface, edit ‘admin.py’ and specify or render the model.

The model is used by the Django admin application to create a site area where we can create, update, view and delete the records. This can save us time during site development by quickly testing our models and seeing if we have the proper data. 

Depending on the type of website, the admin application can also manage data in production. The model-centric approach is not always the greatest possible interface for all users and exposes many unwanted details about the models. The Django project advises it solely for internal data administration (i.e., only for admins or anyone internal in the organization).

This blog focuses on rendering Django Model in the admin interface of the Django web application.

Django Admin Interface

The Django Admin interface is a pre-defined interface designed to save web developers time and expense by eliminating the creation of another admin panel.

Django Admin is a package from the django.contrib package. It is run by the organization and hence does not require a complex frontend.

Django's admin interface offers its own user authentication system and most standard capabilities. It also has several advanced functions, such as authorization access, model management, and CMS (Content Management System).

Rendering Django Model in the Admin Interface

For rendering Django Model in the admin interface, we will first start creating a folder named “Django_Model”.

Setting up the Django Project

We will run the following commands in the terminal:

django-admin startproject django_model_admin
cd django_model_admin
python3 manage.py startapp homepage

 

In the settings.py file, we will add the name of the newly formed app ‘homepage’ to the INSTALLED_APPS list.

django_model_admin/settings.py:

INSTALLED_APPS = [
  'django.contrib.admin',
  'django.contrib.auth',
  'django.contrib.contenttypes',
  'django.contrib.sessions',
  'django.contrib.messages',
  'django.contrib.staticfiles',
  'homepage',
]
You can also try this code with Online Python Compiler
Run Code

Designing the Django Model

A model is simply a database design. Here, we'll build a simple ‘Student’ database model. This is provided in the models.py file in the homepage application's freshly created directory.

homepage/models.py:

from django.db import models

# Create your models here.
class Student(models.Model):
  name = models.CharField(max_length=250)
  rollno = models.CharField(max_length=250)

  def __str__(self):
      return self.name
You can also try this code with Online Python Compiler
Run Code

Setting up Migrations

Django uses the sqlite3 database, as defined in the settings. This can be adjusted to suit our needs.

DATABASES = {
  'default': {
      'ENGINE': 'django.db.backends.sqlite3',
      'NAME': BASE_DIR / 'db.sqlite3',
  }
}
You can also try this code with Online Python Compiler
Run Code

 

Now, we will run the command:

python3 manage.py makemigrations homepage

 

We will get a similar output:

The command ‘makemigrations’ informs Django that some modifications to the specified model have been made and that the changes must be saved as a migration. Django uses ‘migrations’ to keep track of changes to the models and the database schema.

Running the Migrate

Then, to make changes in the database, we will run the migrate command to apply the migrations.

python3 manage.py migrate 

 

We will get a similar output:

According to the database settings specified in the ‘settings.py’ file, the ‘migrate’ command builds necessary database tables for all apps indicated in the INSTALLED_APPS setting.

Entering the Data in Shell

Use the following command to launch the Python shell:

python3 manage.py shell 

 

We can read data and enter/edit data once we're inside the shell as follows:

Running the Server & Django Admin Interface

Django features its built-in admin interface that allows site managers, employees, and clients to manage the website's data. This is not meant for site visitors to use.

First, we will create a user who will use this command to log into the admin site. After that, we will type in our selected username, email address, and password. A dummy email can be used for testing purposes.

python3 manage.py createsuperuser

 

We will then enter the username, email address and password according to our choice.

Now, we will use the command to start the development server.

python3 manage.py runserver

 

The web application is now deployed at ‘http://127.0.0.1:8000/’.

Going to ‘http://127.0.0.1:8000/admin’, we will then login as admin using the ‘username’ and ‘password’ given while creating a superuser.

We will see the Django admin index page upon logging in as admin.

Since we cannot see the Student data here, we will register our ‘Student’ objects with the admin interface. 

homepage/admin.py:

from django.contrib import admin
from .models import Student

# Register your models here.
admin.site.register(Student)
You can also try this code with Online Python Compiler
Run Code

 

The server will immediately refresh after saving the file, and we can now see our Student data in the admin interface.

We can use the admin panel to add new data to the Student section by clicking on it.

The admin interface for Model Student has been successfully rendered! This is how a model displays on the admin interface. In the admin interface, multiple renderings can also be modeled and rendered.

Frequently Asked Questions

1. How to add the ‘Book’ model to the Django admin?

Ans: We can assume that the Django model file is imported correctly into the Django application, then we can add the ‘Book’ model to the Django admin with the following line of code:

admin.site.register(Book)
You can also try this code with Online Python Compiler
Run Code

 

2. What is the Django Admin Interface, and how does it work?

Ans: Django includes an entirely customizable admin interface that allows us to view and edit all of the data stored in the database of registered apps and models. To use the admin interface with a database table, we must first register the model in the admin.py file.

Key Takeaways

We have learned the concept of rendering Django Model in the admin interface of the Django web application. It is the process of introducing a model to the admin interface so that the data can be easily changed.

Go through The Basics of Django Framework to deeply understand the concept of Django, which will be helpful in rendering Django Model in the admin interface.

Credits: GIPHY

Happy Developing!

Live masterclass