Model (Django)
A Django model is a Python class that subclasses django.db.models.Model and represents a database table. Each class attribute is a field (a column), and Django generates the SQL schema and an ORM API from it.
Django
Models are the single source of truth for your data. You define fields like CharField, IntegerField, DateTimeField, and ForeignKey as class attributes, and Django turns the class into a table and gives you a Python API to create, query, update, and delete rows.
Each model automatically gets an id primary key unless you specify your own, and a Manager named objects that is your entry point for queries (Post.objects.all()). You add behavior with methods and customize representation with __str__ and metadata via an inner Meta class (ordering, table name, constraints).
Changing a model does not change the database directly — you run makemigrations to generate a migration and migrate to apply it. This keeps schema changes versioned and reversible.
Syntax
class Post(models.Model):
title = models.CharField(max_length=200)Examples
Python
from django.db import models
class Post(models.Model):
title = models.CharField(max_length=200)
body = models.TextField()
created = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.title