Django Insert Data – Add Records to the Database with Django ORM (2025)
Introduction – What Does “Insert Data” Mean in Django?
In Django, inserting data refers to creating new records in your database using the Django ORM (Object-Relational Mapping). You can do this programmatically via Python code in views, Django shell, or admin—without writing SQL.
In this guide, you’ll learn:
- How to insert data using Python
- How to use the Django shell and views
- How to insert via forms or admin panel
- Best practices for safe and efficient data creation
Step 1: Define Your Model
models.py
from django.db import models
class Post(models.Model):
title = models.CharField(max_length=200)
content = models.TextField()
published = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.title
Run migrations:
$ python manage.py makemigrations
$ python manage.py migrate
Step 2: Insert Data Using Django Shell
$ python manage.py shell
Then:
from blog.models import Post
Post.objects.create(title='First Post', content='This is a blog post.')
This creates a new record instantly.
Step 3: Insert Data from a View
views.py
from django.http import HttpResponse
from blog.models import Post
from datetime import datetime
def insert_post(request):
Post.objects.create(
title='Inserted via View',
content='This data was inserted from a Django view.',
published=datetime.now()
)
return HttpResponse(" Data Inserted!")
Map to URL:
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('insert/', views.insert_post, name='insert-post'),
]
Visit http://localhost:8000/insert/ to trigger the data insertion.
Step 4: Insert via Django Admin Panel
Register your model:
admin.py
from django.contrib import admin
from .models import Post
admin.site.register(Post)
Start server and go to http://localhost:8000/admin/
Add posts using the user-friendly form UI.
Optional: Insert with Model Forms
Use Django’s form system for safe user-submitted data:
from django import forms
from .models import Post
class PostForm(forms.ModelForm):
class Meta:
model = Post
fields = ['title', 'content']
Best Practices
- Always validate user input (especially from forms)
- Use
ModelFormfor user-created content - Avoid inserting data in GET views (use POST for production)
- Use
get_or_create()to avoid duplicates
Summary – Recap & Next Steps
Key Takeaways:
- Use
Model.objects.create()to insert new records - Insert via Django shell, view functions, admin, or forms
- Register models in admin for GUI-based insertion
- Secure user-submitted data with validation
Real-World Relevance:
Whether you’re building a blog, e-commerce store, or CMS—data insertion is the core of dynamic, interactive web apps.
Frequently Asked Questions (FAQ)
What is the difference between create() and save()?
create() = combines instantiation + save
save() = used after setting attributes manually
Can I insert bulk data at once?
Yes:
Post.objects.bulk_create([
Post(title='Post 1', content='...'),
Post(title='Post 2', content='...'),
])
Can I prevent duplicate entries?
Use:
Post.objects.get_or_create(title='Unique Title')
Should I insert data using views?
For testing, yes. In production, prefer POST with forms or APIs.
Is inserting via admin safe?
Yes. It includes built-in form validation and CSRF protection.
Share Now :
