Django is a high-level Python web framework that follows the Model-Template-View (MTV) architectural pattern, enabling rapid development of secure and maintainable websites. Built with the philosophy of "batteries included," Django provides a comprehensive toolkit including an ORM for database interactions, a powerful template engine, robust authentication and authorization, and extensive middleware capabilities. Django 5.2 (current LTS) introduced composite primary keys and async-native ORM methods, and the framework's lazy QuerySet evaluation — where queries hit the database only when explicitly accessed — remains the key mental model for writing efficient code.
What This Cheat Sheet Covers
This topic spans 34 focused tables and 332 indexed concepts, 178 flashcards, 11 practice tests with 394 questions. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.
A jump-to index of every table row in this cheat sheet.
An interactive map of every table and concept in this topic.
Table 1: Model Fields
Fields are the building blocks of every Django model, each one maps to a database column and decides what kind of data it holds, how it validates, and how it renders in a form. Reach here when you're sketching out a model and need to pick the right type, from simple text and numbers to the relationship fields (ForeignKey, ManyToManyField, OneToOneField) that wire your tables together. A few constraints are easy to forget, like CharField requiring max_length and ForeignKey requiring on_delete.
| Field | Example | Description | |
|---|---|---|---|
name = models.CharField(max_length=200) | • Required max_length• stores short text strings • rendered as <input type="text"> in forms. | ||
description = models.TextField() | • Unlimited text storage • rendered as <textarea> in forms• no max_length validation. | ||
age = models.IntegerField() | • Stores integers from -2147483648 to 2147483647 • use BigIntegerField for larger range. | ||
price = models.FloatField() | • Stores floating-point numbers as Python float• use DecimalField for financial precision. | ||
amount = models.DecimalField(max_digits=10, decimal_places=2) | • Required max_digits and decimal_places• precise decimal storage for financial data • avoids float precision issues. | ||
is_active = models.BooleanField(default=True) | • Stores True/False • rendered as checkbox • use null=True for three-state nullable boolean (NullBooleanField was removed in Django 4.0). | ||
signup_date = models.DateField(auto_now_add=True) | • Stores a date • auto_now updates on every save, auto_now_add sets once on creation. | ||
created_at = models.DateTimeField(auto_now_add=True) | • Stores date and time with timezone awareness if USE_TZ=True• uses Python datetime.datetime. | ||
email = models.EmailField(unique=True) | • CharField with email validation • max_length defaults to 254• uses EmailValidator. | ||
website = models.URLField() | • CharField with URL validation • max_length defaults to 200• validates using URLValidator. | ||
data = models.JSONField(default=dict) | • Stores JSON-encoded data • supports native database JSON types • queryable with key lookups. | ||
slug = models.SlugField(unique=True) | • CharField for URL slugs • allows letters, numbers, underscores, hyphens • typically paired with prepopulated_fields in admin. | ||
id = models.UUIDField(primary_key=True, default=uuid.uuid4) | • Stores a universally unique identifier • useful for public-facing IDs instead of sequential integers. | ||
author = models.ForeignKey(User, on_delete=models.CASCADE) | • Many-to-one relationship • requires on_delete (CASCADE, PROTECT, SET_NULL, SET_DEFAULT, DO_NOTHING)• creates database index automatically. | ||
tags = models.ManyToManyField(Tag) | • Many-to-many relationship • creates intermediary table automatically • use through for custom intermediary model. | ||
profile = models.OneToOneField(User, on_delete=models.CASCADE) | • One-to-one relationship • similar to ForeignKey with unique=True• useful for extending a model without inheritance. | ||
document = models.FileField(upload_to='docs/') | • Handles file uploads • upload_to defines storage path relative to MEDIA_ROOT. | ||
photo = models.ImageField(upload_to='photos/') | • FileField with image validation • requires Pillow library • optionally stores width and height. | ||
area = models.GeneratedField( expression=F('side') * F('side'), output_field=models.IntegerField(), db_persist=True) | • Database-computed column (Django 5.0+) • always derived from other fields at database level • db_persist=True stores value; False computes on read. |