ValidationError is Django’s signal that a value or object fails validation. In model validation, it is especially useful when fields depend on one another rather than merely rejecting empty or malformed individual values. section 7.2.1
Override a model’s clean() method, check the relationship between fields, and raise ValidationError when the combination is invalid. For example, a SeekingAd can require a band when seeking a musician. section 7.2.1
Validating a SeekingAd
def clean(self): if self.seeking == MusicianBandChoice.MUSICIAN: if self.band is None: raise ValidationError( "Band field required when seeking a musician") Django calls clean() when using the Admin, but creating an object directly in the REPL does not call it automatically. Override save() to call clean() if programmatic creation must enforce the rule. section 7.2.1
