liveBook · Manning

Model-level validation

As explained in Django in Action

Validation performed by a Django model before its values are saved to the database.

Model-level validation checks Django model values before saving them to the database. It helps ensure data can be stored and can enforce rules the database may not understand. section 4.8.1

Django separates database behavior from model and form behavior: null controls database storage, while blank controls whether Django Admin and web forms require a value. section 4.8.1

Common model-validation patterns include:

  • EmailField checks that a value matches an email-address format, even when the database stores it as ordinary text. section 4.8.1
  • choices restricts a field to predefined values and supplies display labels for Django Admin and web-form select controls. section 4.8.1

Country-code choices

A country field can use choices=[("US", "United States"), ("CA", "Canada"), ("MX", "Mexico")]. Django stores the code and displays the longer label. section 4.8.1

How Django in Action covers it

How deep it goes
Distinguishes pre-save model checks from database checks, including email formats, but leaves implementation database-dependent.
Examples
Uses EmailField formatting and country-code choices to demonstrate validation and displayed labels.
What it assumes
Distinguishes database storage from Django model, Admin, and web-form behavior.
What you can do after
Apply it via Django model fields that enforce formats or restrict values before database storage.
How it connects
Connects model validation with database validation, Django Admin, web forms, EmailField, null, blank, and choices.

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.