← Back to Django Course | Chapter 4: Models & Django ORM Basics | Lesson 3 of 10

Common Model Field Types

Django gives you a toolbox of field types, and each one is shaped for a specific kind of value, like text, whole numbers, or dates.

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.

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

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

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

markup
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. #}
Common Mistakes
  1. Using IntegerField for values that should never be negative (like age) instead of PositiveIntegerField.
  2. Storing dates and times as CharField text instead of DateTimeField, losing the ability to sort or filter by date properly.
  3. Reaching for exotic third-party field packages before checking whether a simple built-in field like CharField or BooleanField already does the job.
Chapter Summary
  • 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.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.