Models are the foundation of any Django application that works with data. They define how your data is structured, stored, and retrieved from the database. In simple terms, models are the bridge between your Python code and your database.
In this tutorial, you will learn what Django models are, how to create them, how they map to database tables, and how to perform basic database operations using Django’s ORM.
A model is a Python class that represents a table in your database.
If you create a Post model:
class Post(models.Model):
title = models.CharField(max_length=200)
content = models.TextField()Django will create a database table like:
| id | title | content |
|---|
So:
Models allow you to:
Without models, your Django app cannot handle dynamic data like:
Django uses an ORM to interact with the database.
Instead of writing SQL like:
SELECT * FROM posts;You write Python:
Post.objects.all()Open your app file:
blog/models.pyfrom django.db import models
class Post(models.Model):
title = models.CharField(max_length=200)
content = models.TextField()
models.Model → base classCharField → short textTextField → long textDjango provides many field types.
name = models.CharField(max_length=100)
description = models.TextField()price = models.FloatField()
quantity = models.IntegerField()created_at = models.DateTimeField()
published_date = models.DateField()is_published = models.BooleanField()image = models.ImageField(upload_to='images/')
file = models.FileField(upload_to='files/')__str__ MethodIt is a good practice to define how your model appears in the admin panel.
class Post(models.Model):
title = models.CharField(max_length=200)
def __str__(self):
return self.titleThis makes objects more readable.
Open:
blog/admin.pyAdd:
from django.contrib import admin
from .models import Post
admin.site.register(Post)Now you can manage posts from the admin panel.
After defining a model, you must apply migrations.
python manage.py makemigrationspython manage.py migrateYou can create data using Django shell.
python manage.py shellfrom blog.models import Post
post = Post(title="First Post", content="Hello Django")
post.save()Now the data is stored in the database.
Post.objects.all()Post.objects.get(id=1)Post.objects.filter(title="First Post")post = Post.objects.get(id=1)
post.title = "Updated Title"
post.save()post = Post.objects.get(id=1)
post.delete()You can pass model data to templates.
from .models import Post
from django.shortcuts import render
def post_list(request):
posts = Post.objects.all()
return render(request, 'blog/posts.html', {'posts': posts})<ul>
{% for post in posts %}
<li>{{ post.title }}</li>
{% endfor %}
</ul>
Models can be linked together.
class Author(models.Model):
name = models.CharField(max_length=100)
class Post(models.Model):
title = models.CharField(max_length=200)
author = models.ForeignKey(Author, on_delete=models.CASCADE)You can customize fields with options.
title = models.CharField(max_length=200, null=True, blank=True)null=True → database allows NULLblank=True → form allows emptydefault= → default valueunique=True → unique fieldclass Post(models.Model):
title = models.CharField(max_length=200)
content = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
is_published = models.BooleanField(default=True)
def __str__(self):
return self.titleAfter editing models, always run:
makemigrations
migrateWithout this, you cannot manage data easily.
Example:
price = models.CharField() ❌
price = models.FloatField() ✅max_lengthCharField requires it.
null → databaseblank → forms__str__Think of a Django model like an Excel table.
In this tutorial, you learned that Django models define how data is structured and stored in the database. You created models, applied migrations, inserted, retrieved, updated, and deleted data, and used models inside views and templates.
Models are essential because they allow your Django application to work with real data instead of static content.
__str__?Django models are the backbone of data-driven applications. Once you understand models, you can build real-world applications like blogs, e-commerce platforms, dashboards, and APIs.