In Django's model fields:
blankcontrols the validation at the application level (Django's own input validation mechanisms)
nullcontrols the validation at the database level
Make a field optional
All fields are required by default.
blank=Truetells Django that the field can be left empty (in forms)
null=Truetells the database that the field can be left empty (
So to make a field optional:
pub_time = models.TimeField(blank=True, null=True)
Make a string-based field optional
For string-based fields, Django saves an empty string when
title = models.CharField(blank=True)
This is because of a convention in Django that recommends the use of
'' to represent the empty value for string-based fields:
nullon string-based fields such as
If a string-based field has
null=True, that means it has two possible values for “no data”:
NULL, and the empty string.
In most cases, it’s redundant to have two possible values for “no data;” the Django convention is to use the empty string, not
SQL queries are easier to write when there is only one possible value for "no data".
But of course, if
'' represent different things in a given use case, then
null=True should be used for string-based fields.