diff options
| author | Matthias Kestenholz <mk@feinheit.ch> | 2017-04-23 12:55:07 +0200 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2017-06-13 20:26:36 -0400 |
| commit | e8c056c31a5b353e7b50a405c00db12c28f4a756 (patch) | |
| tree | f0330bd55cd1913ec79ff0e14ac909b8c9e90f49 /docs/ref/models | |
| parent | 9dd244394236388c3479ab202a0ec31055f7ec09 (diff) | |
Fixed #27434 -- Doc'd how to raise a model validation error for a field not in a model form.
Diffstat (limited to 'docs/ref/models')
| -rw-r--r-- | docs/ref/models/instances.txt | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/docs/ref/models/instances.txt b/docs/ref/models/instances.txt index ae560a6a72..31fde97d1e 100644 --- a/docs/ref/models/instances.txt +++ b/docs/ref/models/instances.txt @@ -311,6 +311,35 @@ pass a dictionary mapping field names to errors:: Finally, ``full_clean()`` will check any unique constraints on your model. +.. admonition:: How to raise field-specific validation errors if those fields don't appear in a ``ModelForm`` + + You can't raise validation errors in ``Model.clean()`` for fields that + don't appear in a model form (a form may limit its fields using + ``Meta.fields`` or ``Meta.exclude``). Doing so will raise a ``ValueError`` + because the validation error won't be able to be associated with the + excluded field. + + To work around this dilemma, instead override :meth:`Model.clean_fields() + <django.db.models.Model.clean_fields>` as it receives the list of fields + that are excluded from validation. For example:: + + class Article(models.Model): + ... + def clean_fields(self, exclude=None): + super().clean_fields(exclude=exclude) + if self.status == 'draft' and self.pub_date is not None: + if exclude and 'status' in exclude: + raise ValidationError( + _('Draft entries may not have a publication date.') + ) + else: + raise ValidationError({ + 'status': _( + 'Set status to draft if there is not a ' + 'publication date.' + ), + }) + .. method:: Model.validate_unique(exclude=None) This method is similar to :meth:`~Model.clean_fields`, but validates all |
