Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #18407 -- Made model field's to_python methods fully accept uni…

…code.

When generating error message in to_python, any unicode string
containing non-ascii characters triggered a UnicodeEncodeError for
most field types.
  • Loading branch information...
commit 0dc904979d5b6df78662653d498c91f4d54f36c2 1 parent 473c272
@claudep claudep authored
View
20 django/db/models/fields/__init__.py
@@ -17,7 +17,7 @@
from django.utils.text import capfirst
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
-from django.utils.encoding import smart_unicode, force_unicode, smart_str
+from django.utils.encoding import smart_unicode, force_unicode
from django.utils.ipv6 import clean_ipv6_address
class NOT_PROVIDED:
@@ -530,7 +530,7 @@ def to_python(self, value):
try:
return int(value)
except (TypeError, ValueError):
- msg = self.error_messages['invalid'] % str(value)
+ msg = self.error_messages['invalid'] % value
raise exceptions.ValidationError(msg)
def validate(self, value, model_instance):
@@ -582,7 +582,7 @@ def to_python(self, value):
return True
if value in ('f', 'False', '0'):
return False
- msg = self.error_messages['invalid'] % str(value)
+ msg = self.error_messages['invalid'] % value
raise exceptions.ValidationError(msg)
def get_prep_lookup(self, lookup_type, value):
@@ -686,8 +686,6 @@ def to_python(self, value):
if isinstance(value, datetime.date):
return value
- value = smart_str(value)
-
try:
parsed = parse_date(value)
if parsed is not None:
@@ -779,8 +777,6 @@ def to_python(self, value):
value = timezone.make_aware(value, default_timezone)
return value
- value = smart_str(value)
-
try:
parsed = parse_datetime(value)
if parsed is not None:
@@ -862,7 +858,7 @@ def to_python(self, value):
try:
return decimal.Decimal(value)
except decimal.InvalidOperation:
- msg = self.error_messages['invalid'] % str(value)
+ msg = self.error_messages['invalid'] % value
raise exceptions.ValidationError(msg)
def _format(self, value):
@@ -967,7 +963,7 @@ def to_python(self, value):
try:
return float(value)
except (TypeError, ValueError):
- msg = self.error_messages['invalid'] % str(value)
+ msg = self.error_messages['invalid'] % value
raise exceptions.ValidationError(msg)
def formfield(self, **kwargs):
@@ -1002,7 +998,7 @@ def to_python(self, value):
try:
return int(value)
except (TypeError, ValueError):
- msg = self.error_messages['invalid'] % str(value)
+ msg = self.error_messages['invalid'] % value
raise exceptions.ValidationError(msg)
def formfield(self, **kwargs):
@@ -1107,7 +1103,7 @@ def to_python(self, value):
return True
if value in ('f', 'False', '0'):
return False
- msg = self.error_messages['invalid'] % str(value)
+ msg = self.error_messages['invalid'] % value
raise exceptions.ValidationError(msg)
def get_prep_lookup(self, lookup_type, value):
@@ -1228,8 +1224,6 @@ def to_python(self, value):
# database backend (e.g. Oracle), so we'll be accommodating.
return value.time()
- value = smart_str(value)
-
try:
parsed = parse_time(value)
if parsed is not None:
View
37 tests/modeltests/validation/test_error_messages.py
@@ -1,3 +1,4 @@
+# -*- encoding: utf-8 -*-
from django.core.exceptions import ValidationError
from django.db import models
from django.utils.unittest import TestCase
@@ -12,8 +13,8 @@ def _test_validation_messages(self, field, value, expected):
def test_autofield_field_raises_error_message(self):
f = models.AutoField(primary_key=True)
- self._test_validation_messages(f, 'foo',
- [u"'foo' value must be an integer."])
+ self._test_validation_messages(f, u'fõo',
+ [u"'fõo' value must be an integer."])
# primary_key must be True. Refs #12467.
with self.assertRaisesRegexp(AssertionError,
"AutoFields must have primary_key=True."):
@@ -21,33 +22,33 @@ def test_autofield_field_raises_error_message(self):
def test_integer_field_raises_error_message(self):
f = models.IntegerField()
- self._test_validation_messages(f, 'foo',
- [u"'foo' value must be an integer."])
+ self._test_validation_messages(f, u'fõo',
+ [u"'fõo' value must be an integer."])
def test_boolean_field_raises_error_message(self):
f = models.BooleanField()
- self._test_validation_messages(f, 'foo',
- [u"'foo' value must be either True or False."])
+ self._test_validation_messages(f, u'fõo',
+ [u"'fõo' value must be either True or False."])
def test_float_field_raises_error_message(self):
f = models.FloatField()
- self._test_validation_messages(f, 'foo',
- [u"'foo' value must be a float."])
+ self._test_validation_messages(f, u'fõo',
+ [u"'fõo' value must be a float."])
def test_decimal_field_raises_error_message(self):
f = models.DecimalField()
- self._test_validation_messages(f, 'foo',
- [u"'foo' value must be a decimal number."])
+ self._test_validation_messages(f, u'fõo',
+ [u"'fõo' value must be a decimal number."])
def test_null_boolean_field_raises_error_message(self):
f = models.NullBooleanField()
- self._test_validation_messages(f, 'foo',
- [u"'foo' value must be either None, True or False."])
+ self._test_validation_messages(f, u'fõo',
+ [u"'fõo' value must be either None, True or False."])
def test_date_field_raises_error_message(self):
f = models.DateField()
- self._test_validation_messages(f, 'foo',
- [u"'foo' value has an invalid date format. "
+ self._test_validation_messages(f, u'fõo',
+ [u"'fõo' value has an invalid date format. "
u"It must be in YYYY-MM-DD format."])
self._test_validation_messages(f, 'aaaa-10-10',
@@ -65,8 +66,8 @@ def test_date_field_raises_error_message(self):
def test_datetime_field_raises_error_message(self):
f = models.DateTimeField()
# Wrong format
- self._test_validation_messages(f, 'foo',
- [u"'foo' value has an invalid format. It must be "
+ self._test_validation_messages(f, u'fõo',
+ [u"'fõo' value has an invalid format. It must be "
u"in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."])
# Correct format but invalid date
@@ -83,8 +84,8 @@ def test_datetime_field_raises_error_message(self):
def test_time_field_raises_error_message(self):
f = models.TimeField()
# Wrong format
- self._test_validation_messages(f, 'foo',
- [u"'foo' value has an invalid format. It must be in "
+ self._test_validation_messages(f, u'fõo',
+ [u"'fõo' value has an invalid format. It must be in "
u"HH:MM[:ss[.uuuuuu]] format."])
# Correct format but invalid time
Please sign in to comment.
Something went wrong with that request. Please try again.