๐ Introduction
After creating a model and applying migrations, the next step is to insert records into the database.
In Django, records can be inserted in multiple ways, but the two most common beginner-friendly methods are:
- Django shell
- Django admin panel
In this program, we will use the Student model created in the previous post and insert records into its database table.
๐ฏ Program Statement
๐ Insert records into the database using Django shell or admin panel.
๐ง Prerequisite
This program assumes that the following model already exists from Post 43.
๐ 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
โ Method 1: Insert Records using Django Shell
โ๏ธ Step 1: Open Django Shell
Run:
python manage.py shell
โ๏ธ Step 2: Import the Model
Type the following inside the shell:
from myapp.models import Student
โ๏ธ Step 3: Create and Save a Record
Method A: Create object first, then save
s1 = Student(name="Charlie", age=21, course="MCA", email="charlie@gmail.com") s1.save()
Method B: Direct create
Student.objects.create(name="Aman", age=22, course="BCA", email="aman@example.com")
โ๏ธ Step 4: View All Inserted Records
Student.objects.all()
Example Output:
<QuerySet [<Student: Charlie>, <Student: Aman>]>
๐ง Explanation
Student(...)creates a model object.save()stores it in the databaseobjects.create()both creates and saves the record in one lineobjects.all()fetches all records
โ Method 2: Insert Records using Django Admin Panel
To insert records using admin, we first need to register the model.
โ๏ธ Step 1: Register the 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
This line tells Django admin to display the Student model in the admin panel.
โ๏ธ Step 2: Create Superuser
Run:
python manage.py createsuperuser
Example:
Username: admin
Email address: admin@example.com
Password: ********
โ๏ธ Step 3: Run Server
python manage.py runserver
โ๏ธ Step 4: Open Admin Panel
Open:
๐ http://127.0.0.1:8000/admin/
Login using the superuser credentials.
โ๏ธ Step 5: Add Student Record
After login:
- Click Students
- Click Add Student
- Enter values like:
- Name: Riya
- Age: 21
- Course: MCA
- Email: riya@example.com
- Click Save
๐ง Explanation
Django admin provides a ready-made interface for adding, editing, and deleting model records.
This is one of the biggest advantages of Django.
๐ Result / Output
After inserting records, the Student table contains entries such as:
| ID | Name | Age | Course | |
|---|---|---|---|---|
| 1 | Charlie | 21 | MCA | charlie@gmail.com |
| 2 | Aman | 22 | BCA | aman@example.com |
| 3 | Riya | 21 | MCA | riya@example.com |
๐ง How It Works
Using Shell
- Open Django shell
- Import model
- Create object
- Save object to database
Using Admin
- Register model in
admin.py - Create superuser
- Login to admin panel
- Add records using GUI
๐ฅ Key Concepts
Import Model in Shell
from myapp.models import Student
Used to access model inside shell.
Save Record
s1.save()
Stores object in database.
Direct Insert
Student.objects.create(...)
Creates and saves in one line.
Admin Registration
admin.site.register(Student)
Makes model visible in admin panel.
Create Superuser
python manage.py createsuperuser
Creates admin login account.
โ ๏ธ Common Errors
โ Model not registered in admin
If you forget to add:
admin.site.register(Student)
the model will not appear in admin panel.
โ Superuser not created
You cannot access admin features without a superuser.
โ Duplicate email error
Since email has unique=True, Django will not allow duplicate email values.
Example invalid insertion:
Student.objects.create(name="Test", age=20, course="MCA", email="charlie@gmail.com")
This may raise a uniqueness error if the email already exists.
โ Forgot to import model in shell
Always import the model before using it:
from myapp.models import Student
โ Not saving object
If you create an object like this:
s1 = Student(name="Nyra", age=20, course="MCA", email="nyra@example.com")
but do not call:
s1.save()
the record will not be stored in the database.
๐งช Practice Questions
- Insert three more student records using shell
- Insert records using only admin panel
- Try inserting a duplicate email and observe the error
- Add one more field in model and insert a record again
- Display all records in shell using
Student.objects.all()
๐ค Viva Questions & Answers
1. What is Django shell?
Django shell is an interactive Python environment connected to the Django project. It allows us to test models and database operations directly.
2. Why do we import the model in Django shell?
We import the model so that we can create, save, update, or query its records from the shell.
3. What is the use of .save() method?
The .save() method stores the created object into the database table.
4. What is objects.create() in Django?
objects.create() is a shortcut method that creates and saves a record in one step.
5. What is Django admin panel?
Django admin panel is a built-in management interface that allows us to manage model data using a web browser.
6. Why do we register a model in admin.py?
We register the model so that it appears in the admin panel and can be managed from there.
7. What is a superuser in Django?
A superuser is an administrator account that has full access to the Django admin panel.
8. What is the difference between shell insertion and admin insertion?
Shell insertion is code-based and useful for testing, while admin insertion is GUI-based and easier for manual data entry.
9. What happens if we insert a duplicate value in a unique field?
Django raises an error because unique fields do not allow duplicate values.
๐ Next Post: Fetch Model Data and Display it Dynamically in Templates
๐ Back to List: Django Programs (60 Questions with Solutions)
Further Reading
Introduction to Django Framework and its Features
Examples of Array Functions in PHP
Registration Form Using PDO in PHP
Inserting Information from Multiple CheckBox Selection in a Database Table in PHP
- Angular
- ASP.NET
- C
- C#
- C++
- CSS
- Dot Net Framework
- HTML
- IoT
- Java
- JavaScript
- Kotlin
- PHP
- Power Bi
- Python
- Scratch 3.0
- TypeScript
- VB.NET
