Model Fields & Field Options

A model field is a class attribute on a Django model that maps to one column in the database table and declares that column's data type, such as CharField for text or IntegerField for whole numbers.

Learn Model Fields & Field Options in our free Django course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…

Part of the free Django course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

In this lesson you'll meet the common field types, learn the options that shape each column — null, blank, default, unique, choices, and db_index — and finally untangle the classic confusion between null and blank.

Every attribute you put on a model is a field , and each field class maps to a database column type. CharField holds short text and requires a max_length ; TextField holds unbounded text; IntegerField and DecimalField hold numbers; BooleanField holds true/false; and DateTimeField holds a timestamp. Specialised text fields like EmailField and SlugField are CharField s with extra validation built in.

A ForeignKey is special: instead of a scalar value it stores a link to a row in another table, turning one model into a child of another.

Every field accepts options that refine its behaviour. default supplies a value when none is given, unique=True forbids duplicate values across rows, choices restricts the field to a fixed set of values, and db_index=True adds a database index to speed up lookups.

These two options look alike but live in different worlds. null is about the database : null=True lets the column hold a real SQL NULL . blank is about forms and validation : blank=True lets a form accept an empty value without complaining. One controls storage, the other controls whether the field is required when a human fills in a form.

The standard rules of thumb are worth memorising:

Fill in the blank so the validator rejects any value longer than the field's max_length .

❌ "CharFields must define a 'max_length' attribute"

You declared a CharField (or SlugField ) without telling Django how wide the column should be.

✅ Fix: add max_length=... , e.g. models.CharField(max_length=200) .

You set null=True on a text field, so now blanks become NULL and you have two kinds of "empty".

✅ Fix: drop null=True and use blank=True instead so empties store as "" .

❌ "duplicate key value violates unique constraint"

A field marked unique=True received a value that already exists in another row.

✅ Fix: ensure the value is unique before saving, or remove unique=True if duplicates are actually allowed.

Combine everything: write a validator that checks a row against several field rules at once and reports each problem it finds.

Lesson complete — you can design any model field with confidence!

You now know the common field types and the columns they create, when to reach for default , unique , choices , and db_index , how a ForeignKey links two tables, and the all-important difference between null (database) and blank (forms).

🚀 Up next: Model Methods, __str__ & Meta — add behaviour and configuration to the models you just designed.

Practice quiz

Which option is required on a CharField?

  • default
  • null
  • max_length
  • unique

Answer: max_length. CharField must define max_length for its column width.

Which field stores large, unbounded text?

  • CharField
  • TextField
  • SlugField
  • IntegerField

Answer: TextField. TextField holds unbounded text with no max_length.

What does null=True control?

  • Whether forms accept an empty value
  • Whether the field is indexed
  • The default value
  • Whether the database column may store SQL NULL

Answer: Whether the database column may store SQL NULL. null is a database-level option allowing SQL NULL.

What does blank=True control?

  • Whether the database column may be NULL
  • Whether the column is unique
  • Whether forms accept an empty value
  • The column type

Answer: Whether forms accept an empty value. blank is a validation-level option for forms.

For an optional text field, the Django convention is to use:

  • null=True only
  • unique=True
  • db_index=True
  • blank=True (store '' not NULL)

Answer: blank=True (store '' not NULL). Prefer blank=True alone so empties store as an empty string.

What does a ForeignKey field store?

  • A scalar number
  • A link to a row in another table
  • Free text
  • A boolean

Answer: A link to a row in another table. ForeignKey stores a link to one row in a related table.

Which ForeignKey argument is required to define delete behaviour?

  • on_delete
  • related_name
  • to_field
  • db_column

Answer: on_delete. ForeignKey requires on_delete, e.g. models.CASCADE.

With choices, what is stored in the database?

  • The human-readable label
  • An index number
  • The first item of each tuple (the value)
  • Both items

Answer: The first item of each tuple (the value). choices stores the first tuple item (the value), not the label.

What does db_index=True do?

  • Makes the field required
  • Creates a database index for faster lookups
  • Sets a default value
  • Allows NULL

Answer: Creates a database index for faster lookups. db_index=True builds an index to speed up filtering.

Which field needs max_digits and decimal_places?

  • IntegerField
  • BooleanField
  • DecimalField
  • EmailField

Answer: DecimalField. DecimalField requires max_digits and decimal_places.