summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorJimmy Song <jaejoon@gmail.com>2013-08-04 19:40:16 +0000
committerTim Graham <timograham@gmail.com>2013-08-05 09:06:36 -0400
commitea7bef318fd42411e3db057e888367ef52fb23c3 (patch)
treeaafaaef022e76edf40ddada21ea331e73c6ac5db /docs
parent06f484dcf9984f2a2223f20422e130844c246800 (diff)
[1.6.x] Fixed #20859 - Clarified Model.clean() example.
Backport of 94d7fed775 from master
Diffstat (limited to 'docs')
-rw-r--r--docs/ref/models/instances.txt22
1 files changed, 13 insertions, 9 deletions
diff --git a/docs/ref/models/instances.txt b/docs/ref/models/instances.txt
index f06866d9a1..c9c01d5679 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,