• 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 Add Model to Django Admin Site [Step-by-step]

Aniruddha Chaudhari/9543/5
Django

Do you want to see all the data submitted by the user on your Django website? That’s exactly you need an admin dashboard where you can see all the models and data entries.

Django has a special admin dashboard feature. In Django, models are defined to store the user data.

As part of the Django tutorial, we are going to see how we can add model to Django admin so that we can see all the model data entries from users.

Step 1: Create model in Django

If you have already created model, you can skip this step.

As an example, let’s take a model called ContactModel which has three fields-

  • name to store the name of the user
  • mobile to store the mobile number of the user
  • and email to store the email ID of the user to contact

You can define the model in the models.py as below.

class ContactModel(models.Model):
    name = models.CharField(max_length=120)
    mobile = models.IntegerField()
    email = models.EmailField()

In an earlier tutorial, we have learned about creating a form in Django. Similarly you can create the contact form to allow users to enter the data that will be saved in this model.

Being admin, we might want to see who all have contacted us by submitting the custom contact form.

Django has admin dashboard feature by which we can see or track all the data models or information.

Once user share the contact detail by filling the contact form, let’s see how we can check the information.

Step 2: Add model to Django admin

You can check the admin.py file in your project app directory. If it is not there, just create one.

Edit admin.py and add below lines of code to register model for admin dashboard.

from django.contrib import admin
from .models import ContactModel

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

Here, we are showing all the model fields in the admin site. You can also customize the list_display list by specifying selected fields.

For example, we want to display only a name and an email ID (and not a mobile number) in the Django admin site.

from django.contrib import admin
from .models import ContactModel

@admin.register(ContactModel)
class RequestDemoAdmin(admin.ModelAdmin):
  list_display = ['name', 'email']

That’s all. Now run your Django project.

Step 3: Execute and run project

If you have created the new model, you have to migrate the models into the database. Execute the below command in the console using manage.py script.

Make Migrations:

python manage.py makemigrations

Migrate:

python manage.py migrate

Now let’s start your Django project by running Django server.

python manage.py runserver

Once it is started, open the Django admin dashboard in the browser.

http://127.0.0.1:8000/admin

Once you login into your admin dashboard, you can see the contact model in your dashboard.

Django site admin

Click on the “Contact model”. You will see all the model data entries.

add model to django admin

Note: Field ID is autogenerated fields that uniquely define each entry in the model.

You can also customize the Django admin dashboard to make it more friendly tracking all the models data.

This is all about how you can add model to Django admin. If you find any difficulty, let me know in the comment section below.

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.

Comments

  • Reply
    abhishek parihar
    March 17, 2022 at 3:05 pm

    Not showing data after submitting the registeration form.

    • Reply
      Aniruddha Chaudhari
      March 20, 2022 at 10:51 pm

      Please share the console log or error. Without that, it is hard to trace the problem.

      • Reply
        abhishek parihar
        March 21, 2022 at 10:27 am

        Good Morning
        sir,
        register form submitting properly but not in the admin without any error,

  • Reply
    assda
    June 25, 2022 at 11:59 am

    Just one question, how did you make the main column? I am having an issue in that.

    • Reply
      Aniruddha Chaudhari
      July 2, 2022 at 4:32 pm

      You have to define the Django model and then add it to the admin dashboard.

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