mirror of
https://github.com/django/django.git
synced 2025-07-20 01:29:11 +00:00
[1.9.x] Followed recommended ValidationError use in docs.
Backport of 0db7e61076116c2d93d61f98ef31690542359e48 from master
This commit is contained in:
parent
205cafd01e
commit
61f1b6ff21
@ -464,6 +464,7 @@ instances::
|
|||||||
|
|
||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
|
||||||
def parse_hand(hand_string):
|
def parse_hand(hand_string):
|
||||||
"""Takes a string of cards and splits into a full hand."""
|
"""Takes a string of cards and splits into a full hand."""
|
||||||
@ -471,7 +472,7 @@ instances::
|
|||||||
p2 = re.compile('..')
|
p2 = re.compile('..')
|
||||||
args = [p2.findall(x) for x in p1.findall(hand_string)]
|
args = [p2.findall(x) for x in p1.findall(hand_string)]
|
||||||
if len(args) != 4:
|
if len(args) != 4:
|
||||||
raise ValidationError("Invalid input for a Hand instance")
|
raise ValidationError(_("Invalid input for a Hand instance"))
|
||||||
return Hand(*args)
|
return Hand(*args)
|
||||||
|
|
||||||
class HandField(models.Field):
|
class HandField(models.Field):
|
||||||
|
@ -16,10 +16,14 @@ different types of fields.
|
|||||||
For example, here's a validator that only allows even numbers::
|
For example, here's a validator that only allows even numbers::
|
||||||
|
|
||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
|
||||||
def validate_even(value):
|
def validate_even(value):
|
||||||
if value % 2 != 0:
|
if value % 2 != 0:
|
||||||
raise ValidationError('%s is not an even number' % value)
|
raise ValidationError(
|
||||||
|
_('%(value)s is not an even number'),
|
||||||
|
params={'value': value},
|
||||||
|
)
|
||||||
|
|
||||||
You can add this to a model field via the field's :attr:`~django.db.models.Field.validators`
|
You can add this to a model field via the field's :attr:`~django.db.models.Field.validators`
|
||||||
argument::
|
argument::
|
||||||
|
Loading…
x
Reference in New Issue
Block a user