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

Defining Model Fields

A field is one labeled box on your model, and it decides what kind of data — like text, a number, or a date — Django will let you put inside it.

Declaring a Field

A field is declared as a class attribute set to a field class instance, such as models.CharField(...). Django uses this to build the matching database column.

Example: Declaring a Field

A field is declared as a class attribute set to a field class instance, such as models.CharField(...). Django uses this to build the matching database column.

markup
from django.db import models


class Product(models.Model):
    name = models.CharField(max_length=100)
    price = 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. #}

Required Field Arguments

Some fields require arguments to work correctly. CharField always needs max_length, since Django needs to know the column's maximum size at the database level.

Note: max_length is about database storage size, not form validation length.

Warning: Omitting max_length on a CharField raises a system check error before you can even migrate.

Example: Required Field Arguments

Some fields require arguments to work correctly. CharField always needs max_length, since Django needs to know the column's maximum size at the database level.

markup
from django.db import models


class Comment(models.Model):
    author_name = models.CharField(max_length=80)
    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. #}

Optional Field Behavior

blank=True controls whether a field can be left empty in forms, while null=True controls whether the database column itself can store NULL. They solve different problems.

Note: For text-based fields, prefer blank=True with an empty string default rather than null=True, to avoid two ways of representing 'no value'.

Example: Optional Field Behavior

blank=True controls whether a field can be left empty in forms, while null=True controls whether the database column itself can store NULL. They solve different problems.

markup
from django.db import models


class Profile(models.Model):
    bio = models.TextField(blank=True, default='')
    nickname = models.CharField(max_length=50, blank=True, null=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. #}

Default Values

The default argument gives a field a starting value whenever a new object is created without explicitly setting that field.

Example: Default Values

The default argument gives a field a starting value whenever a new object is created without explicitly setting that field.

markup
from django.db import models


class Order(models.Model):
    quantity = models.IntegerField(default=1)
    is_paid = 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. #}
Common Mistakes
  1. Forgetting max_length on a CharField, which raises an error at makemigrations time since CharField requires it.
  2. Using CharField for very long text like an article body instead of TextField, which has no length limit.
  3. Leaving a field without a default or null=True, then getting an error when migrating a table that already has rows.
Chapter Summary
  • Fields are class attributes on a model, each one an instance of a field class like CharField or IntegerField.
  • Every field type maps to a specific column type and validation rule in the database.
  • CharField needs max_length; other fields like TextField or IntegerField don't.
  • Fields can take options like default, blank, and null to control their behavior.

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.