Django

Register a Model in Django Admin and Display Records

πŸ“Œ Introduction

Django provides a powerful Admin Interface that allows you to:

  • view records
  • add new data
  • update records
  • delete records

πŸ‘‰ But for this, the model must be registered in admin.py.


🎯 Program Statement

πŸ‘‰ Register a model in Django admin and display records.


🧠 Concept

This program uses:

  • models.py β†’ database structure
  • admin.py β†’ model registration
  • Django admin panel

βš™οΈ Step 1: Model (Already Created)


πŸ“ File: models.py

πŸ”Ή Path:

myproject/myapp/models.py

πŸ”Ή Code:

from django.db import models

class Student(models.Model):
name = models.CharField(max_length=100)
age = models.IntegerField()
course = models.CharField(max_length=100)
email = models.EmailField(unique=True)

def __str__(self):
return self.name

βš™οΈ Step 2: Register Model in Admin


πŸ“ File: admin.py

πŸ”Ή Path:

myproject/myapp/admin.py

πŸ”Ή Code:

from django.contrib import admin
from .models import Student

admin.site.register(Student)

🧠 Explanation

  • admin.site.register(Student) makes the model visible in admin panel
  • Without this, model will not appear in admin

βš™οΈ Step 3: Run Server

python manage.py runserver

βš™οΈ Step 4: Open Admin Panel

πŸ‘‰ Open:

http://127.0.0.1:8000/admin/

βš™οΈ Step 5: Login

πŸ‘‰ Use superuser credentials

(If not created, see next post πŸ‘‡)


🌐 Step 6: Output

After Login:

You will see:

MYAPP
Students

Click β€œStudents”

πŸ‘‰ You will see:

  • list of student records
  • Add button
  • Edit option

Click β€œAdd Student”

Form appears like:

Name: ______
Age: ______
Course: ______
Email: ______

After Saving:

Records will be displayed in admin list.


🧠 How It Works

  1. Model defines database table
  2. Admin registers the model
  3. Django automatically generates admin UI
  4. You can perform CRUD operations directly

πŸ”₯ Key Concepts


Register Model

admin.site.register(Student)

Admin Panel URL

/admin/

Model Display

def __str__(self):
return self.name

Controls how record appears in admin.


⚠️ Common Errors


❌ Model not visible in admin

πŸ‘‰ Forgot:

admin.site.register(Student)

❌ No superuser

πŸ‘‰ Cannot login


❌ str not defined

πŸ‘‰ Admin shows object like:

Student object (1)

❌ Server not running

πŸ‘‰ Admin page will not open


πŸ§ͺ Practice Questions

  1. Register another model
  2. Add more fields in Student model
  3. Modify __str__ method
  4. Add multiple records via admin
  5. Try editing records

🎀 Viva Questions & Answers


1. What is Django admin?

Django admin is a built-in interface that allows developers to manage database records easily.


2. Why do we register a model in admin?

To make the model visible and manageable in the admin interface.


3. What is admin.site.register()?

It registers a model with the Django admin site.


4. What is the purpose of admin panel?

To perform CRUD operations without writing frontend code.


5. What is __str__() method?

It defines how an object is displayed in admin.


6. Can we add records using admin?

Yes, admin provides Add, Edit, and Delete functionality.


7. What happens if model is not registered?

It will not appear in admin panel.


8. What URL opens admin panel?

/admin/


9. Who can access admin panel?

Only authenticated users (superuser/staff).


10. Why is admin important?

It saves time by providing automatic backend interface.


πŸ”— Navigation

πŸ‘‰ Next Post: Customize Admin Interface (list_display, search_fields, filters)
πŸ‘‰ Back to List: Django Programs (60 Questions with Solutions)


Further Reading

Introduction to Django Framework and its Features

Django Practice Exercise

Examples of Array Functions in PHP

Basic Programs in PHP

Registration Form Using PDO in PHP

Inserting Information from Multiple CheckBox Selection in a Database Table in PHP

programmingempire

princites.com

You may also like...

Leave a Reply

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