Django Model Field Types#
Practical list of Django model field types, written exactly in the βField β Examples β Use β Meaningβ
π Text & String Fields#
Field |
Examples |
Use |
Meaning |
|---|---|---|---|
CharField |
|
Short text |
Stores small strings (requires max_length) |
TextField |
|
Long text |
Stores large text (no length limit) |
SlugField |
|
URL-friendly text |
Lowercase, hyphenated strings (e.g. my-post-title) |
EmailField |
|
Email address |
CharField with email validation |
URLField |
|
Website links |
CharField with URL validation |
π’ Numeric Fields#
Field |
Examples |
Use |
Meaning |
|---|---|---|---|
IntegerField |
|
Whole numbers |
Standard integer values |
BigIntegerField |
|
Large integers |
Very large numbers (64-bit) |
SmallIntegerField |
|
Small numbers |
Limited-range integers |
PositiveIntegerField |
|
Non-negative numbers |
Only zero or positive integers |
PositiveSmallIntegerField |
|
Small positive numbers |
Small range, non-negative |
FloatField |
|
Decimal numbers (approximate) |
Floating-point values |
DecimalField |
|
Exact decimal numbers |
Precise values (money, currency) |
Boolean & Logical#
Field |
Examples |
Use |
Meaning |
|---|---|---|---|
BooleanField |
is_active = models.BooleanField(default=True)
|
True / False |
Stores boolean values |
π Date & Time Fields#
Field |
Examples |
Use |
Meaning |
|---|---|---|---|
DateField |
|
Date only |
Stores YYYY-MM-DD |
TimeField |
|
Time only |
Stores HH:MM:SS |
DateTimeField |
|
Date and time |
Timestamp with date + time |
DurationField |
|
Time duration |
Stores timedelta values |
π File & Media Fields#
Field |
Examples |
Use |
Meaning |
|---|---|---|---|
FileField |
|
File upload |
Stores file path |
ImageField |
|
Image upload |
FileField with image validation |
π Relationship Fields#
Field |
Examples |
Use |
Meaning |
|---|---|---|---|
ForeignKey |
|
One-to-Many relationship |
Links one object to another |
OneToOneField |
|
One-to-One relationship |
Exactly one related object |
ManyToManyField |
|
Many-to-Many relationship |
Uses an intermediate join table |
π ID & Auto Fields#
Field |
Examples |
Use |
Meaning |
|---|---|---|---|
AutoField |
|
Auto-increment ID |
Default primary key |
BigAutoField |
|
Large auto ID |
64-bit auto-increment ID |
UUIDField |
|
Unique identifiers |
Stores UUID values |
Special & Advanced Fields#
Field |
Examples |
Use |
Meaning |
|---|---|---|---|
BinaryField |
|
Binary data |
Stores raw binary bytes |
JSONField |
|
JSON data |
Stores structured JSON (PostgreSQL best) |
π Location & Network#
Field |
Examples |
Use |
Meaning |
|---|---|---|---|
IPAddressField |
|
IP addresses |
Stores IPv4/IPv6 |
GenericIPAddressField |
|
Network addresses |
Flexible IP storage |
Field Options (Used With Fields)#
Field Options |
Use |
Meaning |
|---|---|---|
|
Database null |
Allows NULL in DB |
|
Form validation |
Field can be empty in forms |
|
Default value |
Used if no value provided |
|
Uniqueness |
No duplicate values allowed |
|
Fixed options |
Restricts values to choices |
|
Primary key |
Uniquely identifies row |
|
Indexing |
Faster lookups |
Most-Used Field Options (One-Line Examples)
username = models.CharField(max_length=50, unique=True)
bio = models.TextField(blank=True, null=True)
status = models.CharField(max_length=10, choices=STATUS_CHOICES)
email = models.EmailField(db_index=True)
Common Real-World Examples
class Product(models.Model):
name = models.CharField(max_length=200)
description = models.TextField()
price = models.DecimalField(max_digits=8, decimal_places=2)
in_stock = models.BooleanField(default=True)
created_at = models.DateTimeField(auto_now_add=True)
Memory Trick (Interview Gold)#
Category |
Examples |
|---|---|
Text |
CharField, TextField |
Numbers |
IntegerField, DecimalField |
Dates |
DateField, DateTimeField |
Relations |
ForeignKey, ManyToMany |
Files |
FileField, ImageField |
Advanced |
JSONField, UUIDField |