liveBook · Manning

Inter-field validation

As explained in Django in Action

Inter-field validation is validation that checks consistency or dependencies among multiple fields of the same model object.

Inter-field validation checks whether several fields on one model make sense together. Ordinary field validation can reject empty values or invalid choices, but dependencies between fields require model-level logic. section 7.2.1

A SeekingAd must identify the right party

In RiffMates, a SeekingAd uses seeking to indicate whether a musician or band is wanted. Its clean() method can require band when seeking is MUSICIAN, and similarly require musician when seeking is BAND; invalid combinations raise ValidationError. Remember the slightly sneaky framework detail: clean() is not called automatically. section 7.2.1

How Django in Action covers it

How deep it goes
Explains overriding clean() and raising ValidationError, while warning that clean() is not invoked automatically.
Examples
Uses RiffMates’ SeekingAd, seeking choices, musician and band foreign keys, and conditional clean() checks.
What it assumes
Assumes basic ORM field validation and introduces dependencies among model fields through a practical example.
What you can do after
Apply model-level checks by overriding clean() and raising ValidationError for inconsistent field combinations.
How it connects
Connects model validation with field validation, foreign keys, choices, and ValidationError.

Related concepts

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.