Django models define your database schema as Python classes. One model class maps to one database table; instances map to rows. The ORM translates Python to SQL.
Defining a model
from django.db import models
class Article(models.Model):
title = models.CharField(max_length=200)
body = models.TextField()
published = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True)
Manager and objects
Article.objects is the default manager—entry point for queries: Article.objects.all(), .filter(), .get().
Important interview questions and answers
- Q: Model vs table?
A: Model is Python class; Django creates/updates the table via migrations. - Q: Primary key?
A: AutoidBigAutoField unless you set primary_key=True on another field. - Q: Raw SQL when?
A: Rare—ORM covers most cases; use.raw()or cursor for reporting/performance edge cases.
Self-check
- What attribute accesses all rows?
- Which field type stores long text?
Interview prep
- Model vs table?
A model class defines schema in Python; migrations create/update the actual database table.