From 94d7fed7750322e8b80402c1e731bab6d4509f2e Mon Sep 17 00:00:00 2001 From: Jimmy Song Date: Sun, 4 Aug 2013 19:40:16 +0000 Subject: [PATCH] Fixed #20859 - Clarified Model.clean() example. --- AUTHORS | 1 + docs/ref/models/instances.txt | 22 +++++++++++++--------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/AUTHORS b/AUTHORS index ade95df06a..130122c5ea 100644 --- a/AUTHORS +++ b/AUTHORS @@ -543,6 +543,7 @@ answer newbie questions, and generally made Django that much better: smurf@smurf.noris.de Vsevolod Solovyov George Song + Jimmy Song sopel Leo Soto Thomas Sorrel diff --git a/docs/ref/models/instances.txt b/docs/ref/models/instances.txt index 1e7ad1ffdf..cb8570afdc 100644 --- a/docs/ref/models/instances.txt +++ b/docs/ref/models/instances.txt @@ -140,15 +140,19 @@ attributes on your model if desired. For instance, you could use it to automatically provide a value for a field, or to do validation that requires access to more than a single field:: - def clean(self): - import datetime - from django.core.exceptions import ValidationError - # Don't allow draft entries to have a pub_date. - if self.status == 'draft' and self.pub_date is not None: - raise ValidationError('Draft entries may not have a publication date.') - # Set the pub_date for published items if it hasn't been set already. - if self.status == 'published' and self.pub_date is None: - self.pub_date = datetime.date.today() + import datetime + from django.core.exceptions import ValidationError + from django.db import models + + class Article(models.Model): + ... + def clean(self): + # Don't allow draft entries to have a pub_date. + if self.status == 'draft' and self.pub_date is not None: + raise ValidationError('Draft entries may not have a publication date.') + # Set the pub_date for published items if it hasn't been set already. + if self.status == 'published' and self.pub_date is None: + self.pub_date = datetime.date.today() Any :exc:`~django.core.exceptions.ValidationError` exceptions raised by ``Model.clean()`` will be stored in a special key error dictionary key,