mirror of
				https://github.com/django/django.git
				synced 2025-10-31 09:41:08 +00:00 
			
		
		
		
	Fixed #18407 -- Made model field's to_python methods fully accept unicode.
When generating error message in to_python, any unicode string containing non-ascii characters triggered a UnicodeEncodeError for most field types.
This commit is contained in:
		| @@ -17,7 +17,7 @@ from django.utils.functional import curry, total_ordering | ||||
| 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 @@ class AutoField(Field): | ||||
|         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 @@ class BooleanField(Field): | ||||
|             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 @@ class DateField(Field): | ||||
|         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 @@ class DateTimeField(DateField): | ||||
|                 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 @@ class DecimalField(Field): | ||||
|         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 @@ class FloatField(Field): | ||||
|         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 @@ class IntegerField(Field): | ||||
|         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 @@ class NullBooleanField(Field): | ||||
|             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 @@ class TimeField(Field): | ||||
|             # 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: | ||||
|   | ||||
| @@ -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 @@ class ValidationMessagesTest(TestCase): | ||||
|  | ||||
|     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 @@ class ValidationMessagesTest(TestCase): | ||||
|  | ||||
|     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 @@ class ValidationMessagesTest(TestCase): | ||||
|     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 @@ class ValidationMessagesTest(TestCase): | ||||
|     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 | ||||
|   | ||||
		Reference in New Issue
	
	Block a user