liveBook · Manning

Blank

As explained in Django in Action

A Django model-field option that determines whether Django Admin and web forms treat the field as required.

`blank` is a Django model-field option for form-facing requiredness. `blank=False` is the default, so Django Admin and web forms require a value. Set `blank=True` when those interfaces may accept an empty value. section 4.8.1

Do not confuse `blank` with `null`. `null` controls whether the database column can store SQL `NULL`; `blank` controls whether Django Admin and web forms permit an empty value. Django keeps database constraints and model validation separate on purpose. section 4.8.1 section 4.7.1

Choose the options according to where absence is allowed:

  • Use the default `blank=False` when a user must supply the field through Django Admin or a web form. section 4.8.1
  • Use `blank=True` when the Admin UI or forms should allow an empty relationship or field value. section 4.7.1
  • Use `blank=True, null=False` only when application code fills in a value after form submission but before saving; this combination is unusual. section 4.8.1

Adding a field to existing rows

Adding `famous_for = models.CharField(max_length=50)` makes the database require a value because `null=False` is the default. Existing Promoter rows therefore need a migration-time value; a one-off default of `""` can populate them. An empty string is still a value to the database, unlike `NULL`. section 10.1.2 section 4.8.1

How Django in Action covers it

How deep it goes
Defines form-facing behavior and contrasts it with database NULL handling, without detailing form implementation internals.
Examples
Uses an optional empty relationship in Django Admin and a Promoter CharField added through a migration.
What it assumes
Introduces Django model fields, Admin, web forms, SQL NULL, and empty strings through direct comparisons.
What you can do after
Operate/configure it by choosing whether forms permit empty values independently of database NULL storage.
How it connects
Connects blank to null, Django Admin, web forms, model validation, and database validation.

This explainer was generated by Manning's knowledge graph from the text of Django in Action. The section links point to the parts of the book it draws on.