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
| 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. |