What is the difference between Django's null and blank field options?
Learn the difference between Django's null and blank field options, how they affect the database and form validation, and best practices for optional fields.
Expected Interview Answer
In Django, null is a database-level option that controls whether a column may store SQL NULL, while blank is a validation-level option that controls whether a field is allowed to be empty in forms and the admin.
null=True lets the database store NULL for the column; null=False (the default) forbids it. blank=True lets validators and forms accept an empty value; blank=False (the default) makes the field required during validation. They are independent: null affects storage, blank affects form validation. For string-based fields like CharField and TextField, Django convention is to avoid null=True and instead store an empty string, so you typically set only blank=True there; for non-string fields such as DateField or ForeignKey you often need both null=True and blank=True to allow a truly optional value.
- Separates database storage rules from form validation rules
- Lets you make a field optional in forms without touching the schema for strings
- Avoids the two-empty-values problem on text fields
- Gives precise control over optional relationships and dates
- Makes model intent explicit and predictable
AI Mentor Explanation
null and blank are like two different rulebooks: null is the ground's scorebook rule about whether a blank cell may exist for a batter's score, while blank is the umpire's rule about whether a player may take the field without registering. One governs what the record can hold, the other governs what is allowed during play — separate checks on the same player.
Step-by-Step Explanation
Step 1
Know the layers
null operates at the database schema level; blank operates at the form and validation level.
Step 2
Set defaults in mind
Both default to False, meaning columns are NOT NULL and fields are required unless changed.
Step 3
Handle string fields
For CharField/TextField use blank=True alone and avoid null=True to keep a single empty value (empty string).
Step 4
Handle non-string fields
For DateField, IntegerField, or ForeignKey, combine null=True and blank=True to allow a truly optional value.
Step 5
Migrate the change
Changing null alters the schema, so run makemigrations and migrate; changing blank alone does not touch the database.
What Interviewer Expects
- Stating null is database-level and blank is validation-level
- Knowing both default to False
- Explaining why null=True is discouraged on text fields
- Recommending null=True + blank=True for optional non-string fields
- Understanding that changing null requires a migration
Common Mistakes
- Thinking null and blank are interchangeable
- Adding null=True on CharField, creating two possible empty values
- Setting only null=True and being surprised the form still requires the field
- Setting only blank=True on a DateField and hitting a NOT NULL database error
- Forgetting that changing null needs a migration
Best Answer (HR Friendly)
“In Django, null controls whether the database column can be empty, and blank controls whether a form will let you leave the field empty. They work at different levels, so you often set them together for optional fields, except on text fields where blank alone is preferred.”
Code Example
from django.db import models
class Profile(models.Model):
# Text: use blank only, store empty string not NULL
bio = models.TextField(blank=True)
# Optional date: needs both to be truly optional
birth_date = models.DateField(null=True, blank=True)
# Optional relation: both required for an empty value
manager = models.ForeignKey(
'self', null=True, blank=True, on_delete=models.SET_NULL,
related_name='reports',
)Follow-up Questions
- Why is null=True discouraged on CharField and TextField?
- What happens if you set only blank=True on a DateField?
- Does changing blank require a database migration?
- How do null and blank interact with form validation?
- How would you make a ForeignKey optional?
MCQ Practice
1. Which option controls whether a database column can store SQL NULL?
null is the database-level option that determines whether the column may store NULL.
2. For a CharField that should be optional in forms, the Django convention is to set?
Text fields should use blank=True and store an empty string, avoiding null=True so there is only one empty value.
3. What do null and blank default to?
Both default to False, so columns are NOT NULL and fields are required until you change them.
Flash Cards
What does null control? — Whether the database column may store SQL NULL — a storage-level option.
What does blank control? — Whether a field may be empty during form and validation — a validation-level option.
Best practice for text fields? — Use blank=True only; avoid null=True to prevent two empty values.
Optional non-string field? — Set both null=True and blank=True, e.g. on DateField or ForeignKey.