Django Model Fields Cheat Sheet
Django Model field types and field options
API: /api/v1/cheatsheet/django-model-fields-cheat-sheet
Django Model Fields Cheat Sheet
Basic Model Structure
⧉
1 2 3 4 5 6 7 | |
Basic mapping:
Model class → Database table Model field → Database column Model instance → Database row
Text Fields
| Field | Purpose |
|---|---|
| CharField | Short or limited text |
| TextField | Long text |
| EmailField | Email address |
| URLField | Web address |
| SlugField | URL-friendly text |
CharField
Short text with a maximum length:
⧉
1 | |
Common uses:
- Names
- Titles
- Labels
- Codes
- Short descriptions
TextField
Long text:
⧉
1 | |
Common uses:
- Articles
- Comments
- Biographies
- Product descriptions
- Notes
EmailField
Email address:
⧉
1 | |
Django validates the value as an email address.
URLField
Web address:
⧉
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |
Number Fields
| Field | Purpose |
|---|---|
| IntegerField | Whole numbers |
| PositiveIntegerField | Zero or positive whole numbers |
| SmallIntegerField | Smaller whole numbers |
| BigIntegerField | Large whole numbers |
| FloatField | Floating-point numbers |
| DecimalField | Fixed-precision decimal numbers |
IntegerField
Whole numbers:
⧉
1 | |
Example values:
- -10
- 0
- 25
PositiveIntegerField
Zero or positive whole numbers:
⧉
1 | |
Common uses:
- Stock quantities
- Page counts
- View counts
- Ages
SmallIntegerField
Whole numbers with a smaller database range:
⧉
1 | |
BigIntegerField
Large whole numbers:
⧉
1 | |
FloatField
Floating-point number:
⧉
1 | |
Useful for approximate numeric values.
Do not normally use it for money.
DecimalField
Fixed-precision decimal number:
⧉
1 2 3 4 | |
Options:
- max_digits Total number of digits
- decimal_places Digits after the decimal point
With the example above:
- Valid: 123456.78
- Invalid: 1234567.89
Common uses:
- Prices
- Account balances
- Measurements requiring fixed precision
- Boolean Fields
BooleanField
Stores True or False:
⧉
1 | |
Common uses:
- Published or unpublished
- Active or inactive
- Enabled or disabled
- Completed or incomplete
Date and Time Fields
| Field | Purpose |
|---|---|
| DateField | Date only |
| TimeField | Time only |
| DateTimeField | Date and time |
| DurationField | Length of time |
DateField
Stores a date:
⧉
1 | |
Example:
2026-07-24
TimeField
Stores a time:
⧉
1 | |
Example:
09:30:00
DateTimeField
Stores a date and time:
⧉
1 | |
Automatically set when the object is created:
⧉
1 | |
Automatically update whenever the object is saved:
⧉
1 | |
- auto_now_add=True Set once during creation
- auto_now=True Update on every save
DurationField
Stores a period of time:
⧉
1 | |
Python normally represents the value as a timedelta.
File Fields
| Field | Purpose |
|---|---|
| FileField | Uploaded files |
| ImageField | Uploaded images |
FileField
Stores an uploaded file path:
⧉
1 | |
Example storage path:
documents/report.pdf
ImageField
Stores an uploaded image path:
⧉
1 | |
ImageField also validates that the uploaded file is an image.
Pillow is normally required:
⧉
1 | |
Dynamic Upload Paths
⧉
1 2 3 4 5 | |
Identifier Fields
| Field | Purpose |
|---|---|
| AutoField | Auto-incrementing integer |
| BigAutoField | Large auto-incrementing integer |
| UUIDField | UUID identifier |
Automatic Primary Key
Django adds a primary key when one is not declared:
⧉
1 2 | |
The model receives an automatic id field.
Access it with:
- product.id
- product.pk
UUIDField
Stores a UUID:
⧉
1 2 3 4 5 6 7 8 9 10 11 | |
Example UUID:
550e8400-e29b-41d4-a716-446655440000
Pass the function itself:
⧉
1 | |
Do not call it:
⧉
1 | |
Other Useful Fields
| Field | Purpose |
|---|---|
| BinaryField | Raw binary data |
| GenericIPAddressField | IPv4 or IPv6 address |
| JSONField | JSON-compatible data |
GenericIPAddressField
Stores an IP address:
⧉
1 | |
Possible values:
- 192.168.1.10
- 2001:db8::1
JSONField
Stores JSON-compatible data:
⧉
1 | |
Example value:
⧉
1 2 3 4 | |
Use a callable for mutable defaults:
⧉
1 2 | |
Do not use a shared object:
⧉
1 | |
Relationship Fields
| Field | Relationship |
|---|---|
| ForeignKey | Many-to-one |
| OneToOneField | One-to-one |
| ManyToManyField | Many-to-many |
ForeignKey
Many objects relate to one object.
Example:
⧉
1 2 3 4 5 6 7 8 9 10 | |
Meaning:
- One category can contain many products.
- Each product has one category.
Access the category:
⧉
1 | |
Access products from a category:
⧉
1 | |
Add a custom reverse name:
⧉
1 2 3 4 5 | |
Then use:
⧉
1 | |
OneToOneField
One object relates to one object.
⧉
1 2 3 4 5 6 7 | |
Meaning:
- One user has one profile.
- One profile belongs to one user.
ManyToManyField
Many objects relate to many objects.
⧉
1 2 3 4 5 6 7 | |
Meaning:
- One article can have many tags.
- One tag can belong to many articles.
Add relationships:
⧉
1 | |
Remove relationships:
⧉
1 | |
Retrieve related objects:
⧉
1 | |
Optional many-to-many field:
⧉
1 | |
null=True is not used for ManyToManyField.
on_delete Options
Used by ForeignKey and OneToOneField.
| Option | Behavior |
|---|---|
| CASCADE | Delete related objects |
| PROTECT | Block deletion |
| RESTRICT | Restrict deletion |
| SET_NULL | Set the field to NULL |
| SET_DEFAULT | Set the field to its default |
| SET(...) | Set a custom value |
| DO_NOTHING | Take no automatic action |
CASCADE
⧉
1 2 3 4 | |
Deleting the category also deletes its products.
PROTECT
⧉
1 2 3 4 | |
Django blocks deletion while related products exist.
SET_NULL
⧉
1 2 3 4 5 | |
Deleting the category sets the product’s category to NULL.
null=True is required.
SET_DEFAULT
⧉
1 2 3 4 5 | |
Deleting the category sets the field to its default value.
Common Field Options
| Option | Purpose |
|---|---|
| null | Allow database NULL |
| blank | Allow an empty value during validation |
| default | Provide a default value |
| unique | Require unique values |
| choices | Restrict allowed values |
| primary_key | Make the field the primary key |
| db_index | Create a database index |
| editable | Include or exclude from forms and admin |
| help_text | Add explanatory text |
| verbose_name | Set a human-readable field name |
| validators | Add validation functions |
| error_messages | Customize validation messages |
null
Controls database storage:
⧉
1 | |
- null=False Database value is required
- null=True Database may store NULL
Default:
⧉
1 | |
For optional dates:
⧉
1 2 3 4 | |
For optional text, normally use:
⧉
1 | |
Avoid this in most cases:
⧉
1 2 3 4 | |
Using null=True on text fields creates two empty values:
NULL ""
blank
Controls validation:
⧉
1 | |
- blank=False Required in forms and validation
- blank=True May be left empty
Default:
⧉
1 | |
Remember:
null → Database blank → Validation
default
Provides a value when none is supplied:
⧉
1 2 | |
Callable default:
⧉
1 2 3 | |
Do not call the function:
⧉
1 2 | |
Mutable defaults must use callables:
⧉
1 | |
unique
Requires a unique value:
⧉
1 | |
Duplicate values are rejected.
Common uses:
- Usernames
- Email addresses
- Slugs
- Reference numbers
- External IDs
choices
Limits a field to predefined values:
⧉
1 2 3 4 5 6 7 8 9 10 | |
Stored value:
draft
Displayed label:
Draft
Get the display label:
⧉
1 | |
TextChoices
⧉
1 2 3 4 5 6 7 8 9 10 | |
Use:
⧉
1 2 | |
primary_key
Makes a field the primary key:
⧉
1 2 3 4 | |
A primary key is automatically:
- Unique
- Required
- Indexed
Most models can use Django’s automatic primary key.
db_index
Creates a database index:
⧉
1 2 3 4 | |
Indexes can improve searches and filtering:
⧉
1 | |
Indexes use additional storage and can slow writes.
Use them for fields that are queried frequently.
editable
Controls whether the field appears in model forms and the admin:
⧉
1 2 3 4 | |
help_text
Provides instructions:
⧉
1 2 3 | |
The text may appear in forms and the Django admin.
verbose_name
Sets a readable field label:
⧉
1 2 3 4 | |
Or:
⧉
1 2 3 4 | |
validators
Adds custom validation:
⧉
1 2 3 4 5 6 | |
Multiple validators:
⧉
1 2 3 4 5 6 7 8 9 10 11 12 | |
Field-Specific Options
| Option | Used with |
|---|---|
| max_length | CharField and similar fields |
| max_digits | DecimalField |
| decimal_places | DecimalField |
| upload_to | FileField and ImageField |
| auto_now | DateField and DateTimeField |
| auto_now_add | DateField and DateTimeField |
| on_delete | ForeignKey and OneToOneField |
| related_name | Relationship fields |
| to_field | ForeignKey and OneToOneField |
| through | ManyToManyField |
max_length
Maximum text length:
⧉
1 | |
Required for CharField.
upload_to
Sets the upload directory:
⧉
1 2 3 | |
Date-based path:
⧉
1 2 3 | |
Possible path:
- products/2026/07/photo.jpg
related_name
Sets the reverse relationship name:
⧉
1 2 3 4 5 6 | |
Reverse query:
⧉
1 | |
Without related_name:
⧉
1 | |
Complete Example
⧉
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | |
Common Combinations
Required Short Text
⧉
1 | |
Optional Short Text
⧉
1 2 3 4 | |
Optional Long Text
⧉
1 | |
Optional Date
⧉
1 2 3 4 | |
Unique Slug
⧉
1 | |
Price
⧉
1 2 3 4 | |
Creation Timestamp
⧉
1 2 3 | |
Update Timestamp
⧉
1 2 3 | |
Optional File
⧉
1 2 3 4 | |
Optional Relationship
⧉
1 2 3 4 5 6 | |
Required Protected Relationship
⧉
1 2 3 4 | |
Common Mistakes
Confusing null and blank
null → Controls database NULL blank → Controls validation
Using FloatField for Money
Avoid:
⧉
1 | |
Prefer:
⧉
1 2 3 4 | |
Using a Mutable Default Directly
Avoid:
⧉
1 | |
Prefer:
⧉
1 | |
Calling a Default Function
Avoid:
⧉
1 | |
Prefer:
⧉
1 | |
Adding null=True to Text Fields
Usually avoid:
⧉
1 2 3 4 5 | |
Prefer:
⧉
1 2 3 4 | |
Forgetting on_delete
Incorrect:
⧉
1 | |
Correct:
⧉
1 2 3 4 | |
Using null=True on ManyToManyField
Avoid:
⧉
1 2 3 4 | |
Use:
⧉
1 2 3 4 | |
Mini Reference Summary
CharField → Short text TextField → Long text IntegerField → Whole number DecimalField → Fixed-precision number BooleanField → True or False DateField → Date DateTimeField → Date and time EmailField → Email address URLField → Web address SlugField → URL-friendly text FileField → Uploaded file ImageField → Uploaded image JSONField → JSON data UUIDField → UUID identifier ForeignKey → Many-to-one OneToOneField → One-to-one ManyToManyField → Many-to-many null → Database NULL blank → Empty validation value default → Default value unique → No duplicate values choices → Limited allowed values primary_key → Main record identifier db_index → Database index related_name → Reverse relationship name on_delete → Related deletion behavior
Join the Newsletter
Practical insights on Django, backend systems, deployment, architecture, and real-world development — delivered without noise.
Get updates when new guides, learning paths, cheat sheets, and field notes are published.
No spam. Unsubscribe anytime.
There is no third-party involved so don't worry - we won't share your details with anyone.