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=True and null=True => empty field value allowed in forms and will be null in the database

  • blank=True and null=False => empty field value allowed in forms and will be stored as an empty string in the database

  • blank=False and null=True => empty field value not allowed Obviously those two options don't make logical sense to use (though, there might be a use case for null=True, blank=False if you want a field to always be required in forms, but optional when dealing with an object through something like the shell.)

  • blank=False and null=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

Was this helpful?