mirror of
https://github.com/django/django.git
synced 2025-01-05 07:55:47 +00:00
Fixed #20859 - Clarified Model.clean() example.
This commit is contained in:
parent
26c4bd38ac
commit
94d7fed775
1
AUTHORS
1
AUTHORS
@ -543,6 +543,7 @@ answer newbie questions, and generally made Django that much better:
|
|||||||
smurf@smurf.noris.de
|
smurf@smurf.noris.de
|
||||||
Vsevolod Solovyov
|
Vsevolod Solovyov
|
||||||
George Song <george@damacy.net>
|
George Song <george@damacy.net>
|
||||||
|
Jimmy Song <jaejoon@gmail.com>
|
||||||
sopel
|
sopel
|
||||||
Leo Soto <leo.soto@gmail.com>
|
Leo Soto <leo.soto@gmail.com>
|
||||||
Thomas Sorrel
|
Thomas Sorrel
|
||||||
|
@ -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
|
automatically provide a value for a field, or to do validation that requires
|
||||||
access to more than a single field::
|
access to more than a single field::
|
||||||
|
|
||||||
def clean(self):
|
import datetime
|
||||||
import datetime
|
from django.core.exceptions import ValidationError
|
||||||
from django.core.exceptions import ValidationError
|
from django.db import models
|
||||||
# Don't allow draft entries to have a pub_date.
|
|
||||||
if self.status == 'draft' and self.pub_date is not None:
|
class Article(models.Model):
|
||||||
raise ValidationError('Draft entries may not have a publication date.')
|
...
|
||||||
# Set the pub_date for published items if it hasn't been set already.
|
def clean(self):
|
||||||
if self.status == 'published' and self.pub_date is None:
|
# Don't allow draft entries to have a pub_date.
|
||||||
self.pub_date = datetime.date.today()
|
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
|
Any :exc:`~django.core.exceptions.ValidationError` exceptions raised by
|
||||||
``Model.clean()`` will be stored in a special key error dictionary key,
|
``Model.clean()`` will be stored in a special key error dictionary key,
|
||||||
|
Loading…
Reference in New Issue
Block a user