Common Model Field Types
In this page:
Text Fields
CharField is for short, single-line text with a maximum length, while TextField is for long text like paragraphs, with no length restriction.
Example: Text Fields
CharField is for short, single-line text with a maximum length, while TextField is for long text like paragraphs, with no length restriction.
from django.db import models
class Article(models.Model):
headline = models.CharField(max_length=120)
body = models.TextField()
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Numeric Fields
IntegerField stores whole numbers. Django also has FloatField for decimals, but for simple counters and quantities IntegerField is usually enough.
Example: Numeric Fields
IntegerField stores whole numbers. Django also has FloatField for decimals, but for simple counters and quantities IntegerField is usually enough.
from django.db import models
class Inventory(models.Model):
item_name = models.CharField(max_length=100)
quantity = models.IntegerField()
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Boolean Fields
BooleanField stores a simple True or False value, perfect for flags like whether a task is complete or a user is active.
Example: Boolean Fields
BooleanField stores a simple True or False value, perfect for flags like whether a task is complete or a user is active.
from django.db import models
class Task(models.Model):
title = models.CharField(max_length=150)
is_complete = models.BooleanField(default=False)
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Date and Time Fields
DateTimeField stores both a date and a time. auto_now_add=True stamps it automatically once, when the object is first created.
Note: Use auto_now_add for a creation timestamp and auto_now for an updated-at timestamp — never both on the same field.
Example: Date and Time Fields
DateTimeField stores both a date and a time. auto_now_add=True stamps it automatically once, when the object is first created.
from django.db import models
class LogEntry(models.Model):
message = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
- Using IntegerField for values that should never be negative (like age) instead of PositiveIntegerField.
- Storing dates and times as CharField text instead of DateTimeField, losing the ability to sort or filter by date properly.
- Reaching for exotic third-party field packages before checking whether a simple built-in field like CharField or BooleanField already does the job.
- CharField stores short text and needs max_length; TextField stores long text with no limit.
- IntegerField stores whole numbers; BooleanField stores True/False values.
- DateTimeField stores a combined date and time, often paired with auto_now_add or auto_now.
- Picking the right field type keeps validation and database storage correct from the start.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: