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 330 indexed concepts. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.
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). | |
birth_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. |