Django models: difference between null and blank
Fields in Django models take certain arguments. Null and blank are ones that are available to all field types, and are sometimes confusing as to which one means what.
NULL
If True, Django will store empty values as NULL in the database. Default is False.
BLANK
If True, the field is allowed to be blank. Default is False.
Note that this is different than null.
null is purely database-related, whereas blank is validation-related.
If a field has
blank=True, form validation will allow entry of an empty value.If a field has
blank=False, the field will be required.
Examples
blank=Trueandnull=True=> empty field value allowed in forms and will benullin the databaseblank=Trueandnull=False=> empty field value allowed in forms and will be stored as an empty string in the databaseblank=Falseandnull=True=> empty field value not allowed Obviously those two options don't make logical sense to use (though, there might be a use case fornull=True, blank=Falseif you want a field to always be required in forms, but optional when dealing with an object through something like the shell.)blank=Falseandnull=False=> a.k.a the default. Empty field value not allowed in forms, but if appears somehow (e.g. from the shell) will be stored as an empty string in the database
Good SO discussion: https://stackoverflow.com/questions/8609192/differentiate-null-true-blank-true-in-django
Last updated