Django Insert Data

In Django, inserting data into the database involves creating an object (instance) of a model and saving it. This process writes the object’s data into the corresponding table in the database.

Create a Django Model Book


from django.db import models

class Book(models.Model):
    title = models.CharField(max_length=200)  # Title of the book
    author = models.CharField(max_length=100)  # Author name
    published_year = models.IntegerField()     # Year the book was published

    def __str__(self):
        return self.title

How to Insert Data into the Database

Using Django Shell

1. Open the Django shell:


python manage.py shell

2. Import the model:


from myapp.models import Book  # Replace 'myapp' with your app name

3. Create and save a new book instance:


# Create a book instance
new_book = Book(
    title="A Suitable Boy",
    author="Vikram Seth",
    published_year=1993
)

# Save the book to the database
new_book.save()

Now, the new book is saved in the database.

Using create() Method

Instead of creating and saving separately, you can use the create() method, which does both in one step:


Book.objects.create(
    title="The White Tiger",
    author="Aravind Adiga",
    published_year=2008
)

Insert Multiple Data

1. Using bulk_create() for Multiple Inserts

The bulk_create() method is used to insert multiple records at once. It’s faster than saving each instance individually because it sends a single query to the database.

Example:


from myapp.models import Book  # Replace 'myapp' with your app name

# Create a list of Book instances (do not save yet)
books = [
    Book(title="The Inheritance of Loss", author="Kiran Desai", published_year=2007),
    Book(title="The Guide", author="R. K. Narayan", published_year=1958),
    Book(title="The Palace of Illusions", author="Chitra Banerjee Divakaruni", published_year=2008),
]

# Use bulk_create to insert all the books at once
Book.objects.bulk_create(books)

Explanation:

A list of Book instances is created but not saved.

bulk_create() inserts all the instances in one database query, which is efficient.

2. Using Loops to Insert Multiple Records

If you need custom logic for each instance, you can use a loop with .save(). However, this sends one query per record, which is slower for large datasets.

Example:


from myapp.models import Book  # Replace 'myapp' with your app name

# List of data to insert
book_data = [
    {"title": "The Inheritance of Loss", "author": "Kiran Desai", "published_year": 2007},
    {"title": "The Guide", "author": "R. K. Narayan", "published_year": 1958},
    {"title": "The Palace of Illusions", "author": "Chitra Banerjee Divakaruni", "published_year": 2008},
]

# Loop through the data and save each record
for data in book_data:
    Book.objects.create(**data)

Explanation:

The create() method saves each record one at a time.

**data unpacks the dictionary keys into model fields.

Django Insert Data – Interview Questions

Q 1: How do you insert data using Django ORM?
Ans: By creating and saving model objects.
Q 2: What does save() do?
Ans: Inserts or updates data in the database.
Q 3: Can data be inserted via Django admin?
Ans: Yes.
Q 4: What is bulk_create()?
Ans: Inserts multiple records at once.
Q 5: Can forms be used to insert data?
Ans: Yes, using Django forms.

Django Insert Data– Objective Questions (MCQs)

Q1. Which method is commonly used to insert a new object into the database?






Q2. To insert data using ORM, which statement is correct?






Q3. To save a new record using object creation, you use:






Q4. Which file typically contains the form logic for inserting data?






Q5. During data insertion, Django automatically protects against:






Related Django Insert Data Topics