If you define null=True
(unlike NOT NULL) in a field of your template (a BD column), when entering empty values for types like DateTimeField
and ForeignKey
will be stored as NULL in your BD, this is very useful for Fks, when you want the related item to be left blank.
Defining blank=True
you do not require the field to be filled in the forms, and may remain blank if you define blank=False
this field shall be mandatory, that is, it shall not remain blank.
The combination of the two, at first glance, may not seem to make sense, but if you look at it from the perspective of front-end harmony with the back-end, you will see that if you allow a field to be blank (ie omitted) in a form, you will also need the database to allow NULL values for this field, with the exception of CharFields
and TextFields
which are stored as an empty string ('') instead of NULL, in Django.
In the documentation has the following note in explaining blank
:
Note that blank
is different from null. null
is specifically related to the database, while the blank
is related to validation. If a field has blank=True
, the validation of a form will allow the entry of an empty value. If the field has blank=False
, then the field will be mandatory.
Thank you, you helped me a lot!!
– Renan