Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[1.6.x] Fixed #20859 - Clarified Model.clean() example.

Backport of 94d7fed from master
  • Loading branch information...
commit ea7bef318fd42411e3db057e888367ef52fb23c3 1 parent 06f484d
@jimmysong jimmysong authored timgraham committed
Showing with 14 additions and 9 deletions.
  1. +1 −0  AUTHORS
  2. +13 −9 docs/ref/models/instances.txt
View
1  AUTHORS
@@ -540,6 +540,7 @@ answer newbie questions, and generally made Django that much better:
smurf@smurf.noris.de
Vsevolod Solovyov
George Song <george@damacy.net>
+ Jimmy Song <jaejoon@gmail.com>
sopel
Leo Soto <leo.soto@gmail.com>
Wiliam Alves de Souza <wiliamsouza83@gmail.com>
View
22 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,
Please sign in to comment.
Something went wrong with that request. Please try again.