Ticket #11716: 11716-2.patch
File 11716-2.patch, 8.6 KB (added by , 15 years ago) |
---|
-
django/contrib/contenttypes/views.py
1 1 from django import http 2 2 from django.contrib.contenttypes.models import ContentType 3 3 from django.contrib.sites.models import Site 4 from django.core.exceptions import ObjectDoesNotExist 4 from django.core.exceptions import ObjectDoesNotExist, ValidationError 5 5 6 6 def shortcut(request, content_type_id, object_id): 7 7 "Redirect to an object's page based on a content-type ID and an object ID." … … 9 9 try: 10 10 content_type = ContentType.objects.get(pk=content_type_id) 11 11 obj = content_type.get_object_for_this_type(pk=object_id) 12 except (ObjectDoesNotExist, Val ueError):12 except (ObjectDoesNotExist, ValidationError): 13 13 raise http.Http404("Content type %s object %s doesn't exist" % (content_type_id, object_id)) 14 14 try: 15 15 absurl = obj.get_absolute_url() -
django/db/models/fields/__init__.py
495 495 pass 496 496 497 497 def get_prep_value(self, value): 498 if value is None: 499 return None 500 return int(value) 498 return self.to_python(value) 501 499 502 500 def contribute_to_class(self, cls, name): 503 501 assert not cls._meta.has_auto_field, "A model can't have more than one AutoField." … … 544 542 return super(BooleanField, self).get_prep_lookup(lookup_type, value) 545 543 546 544 def get_prep_value(self, value): 547 if value is None: 548 return None 549 return bool(value) 545 return self.to_python(value) 550 546 551 547 def formfield(self, **kwargs): 552 548 # Unlike most fields, BooleanField figures out include_blank from … … 848 844 description = _("Floating point number") 849 845 850 846 def get_prep_value(self, value): 851 if value is None: 852 return None 853 return float(value) 847 return self.to_python(value) 854 848 855 849 def get_internal_type(self): 856 850 return "FloatField" … … 871 865 class IntegerField(Field): 872 866 empty_strings_allowed = False 873 867 default_error_messages = { 874 'invalid': _("This value must be a float."),868 'invalid': _("This value must be an integer."), 875 869 } 876 870 description = _("Integer") 877 871 878 872 def get_prep_value(self, value): 879 if value is None: 880 return None 881 return int(value) 873 return self.to_python(value) 882 874 883 875 def get_internal_type(self): 884 876 return "IntegerField" … … 961 953 return super(NullBooleanField, self).get_prep_lookup(lookup_type, value) 962 954 963 955 def get_prep_value(self, value): 964 if value is None: 965 return None 966 return bool(value) 956 return self.to_python(value) 967 957 968 958 def formfield(self, **kwargs): 969 959 defaults = { -
django/forms/models.py
999 999 for pk in value: 1000 1000 try: 1001 1001 self.queryset.filter(pk=pk) 1002 except Val ueError:1002 except ValidationError: 1003 1003 raise ValidationError(self.error_messages['invalid_pk_value'] % pk) 1004 1004 qs = self.queryset.filter(pk__in=value) 1005 1005 pks = set([force_unicode(o.pk) for o in qs]) -
tests/modeltests/expressions/models.py
127 127 >>> acme.save() 128 128 Traceback (most recent call last): 129 129 ... 130 TypeError: int() argument must be a string or a number... 130 ValidationError: [u'This value must be an integer.'] 131 131 132 132 """} -
tests/regressiontests/model_fields/tests.py
25 25 ImageFieldUsingFileTests, \ 26 26 TwoImageFieldTests 27 27 28 28 class AutoFieldTests(django.test.TestCase): 29 def test_to_python(self): 30 f = models.AutoField(primary_key=True) 31 self.assertEqual(f.to_python(3), 3) 32 self.assertEqual(f.to_python("3"), 3) 33 self.assertEqual(f.to_python(None), None) 34 self.assertRaises(ValidationError, f.to_python, "abc") 35 36 def test_get_db_prep_value(self): 37 f = models.AutoField(primary_key=True) 38 self.assertEqual(f.get_db_prep_value(3), 3) 39 self.assertEqual(f.get_db_prep_value("3"), 3) 40 self.assertEqual(f.get_db_prep_value(None), None) 41 self.assertRaises(ValidationError, f.get_db_prep_value, "abc") 42 29 43 class DecimalFieldTests(django.test.TestCase): 30 44 def test_to_python(self): 31 45 f = models.DecimalField(max_digits=4, decimal_places=2) 32 46 self.assertEqual(f.to_python(3), Decimal("3")) 33 47 self.assertEqual(f.to_python("3.14"), Decimal("3.14")) 34 self.assertRaises(ValidationError, f.to_python, "abc") 48 self.assertEqual(f.to_python(None), None) 49 self.assertRaises(ValidationError, f.to_python, "abc") 35 50 51 def test_get_db_prep_value(self): 52 f = models.DecimalField(max_digits=4, decimal_places=2) 53 self.assertEqual(f.get_db_prep_value(3), 3) 54 self.assertEqual(f.get_db_prep_value("3.14"), Decimal("3.14")) 55 self.assertEqual(f.to_python(None), None) 56 self.assertRaises(ValidationError, f.get_db_prep_value, "abc") 57 36 58 def test_default(self): 37 59 f = models.DecimalField(default=Decimal("0.00")) 38 60 self.assertEqual(f.get_default(), Decimal("0.00")) … … 73 95 # This should not crash. That counts as a win for our purposes. 74 96 Foo.objects.filter(d__gte=100000000000) 75 97 98 class IntegerFieldTests(django.test.TestCase): 99 def test_to_python(self): 100 f = models.IntegerField() 101 self.assertEqual(f.to_python(3), 3) 102 self.assertEqual(f.to_python("3"), 3) 103 self.assertEqual(f.to_python(None), None) 104 self.assertRaises(ValidationError, f.to_python, "abc") 105 106 def test_get_db_prep_value(self): 107 f = models.IntegerField() 108 self.assertEqual(f.get_db_prep_value(3), 3) 109 self.assertEqual(f.get_db_prep_value("3"), 3) 110 self.assertEqual(f.get_db_prep_value(None), None) 111 self.assertRaises(ValidationError, f.get_db_prep_value, "abc") 112 113 class FloatFieldTests(django.test.TestCase): 114 def test_to_python(self): 115 f = models.FloatField() 116 self.assertEqual(f.to_python(3), float("3")) 117 self.assertEqual(f.to_python("3.14"), float("3.14")) 118 self.assertEqual(f.to_python(None), None) 119 self.assertRaises(ValidationError, f.to_python, "abc") 120 121 def test_get_db_prep_value(self): 122 f = models.FloatField() 123 self.assertEqual(f.get_db_prep_value(3), float(3)) 124 self.assertEqual(f.get_db_prep_value("3.14"), float("3.14")) 125 self.assertEqual(f.to_python(None), None) 126 self.assertRaises(ValidationError, f.get_db_prep_value, "abc") 127 76 128 class ForeignKeyTests(django.test.TestCase): 77 129 def test_callable_default(self): 78 130 """Test the use of a lazy callable for ForeignKey.default""" … … 106 158 self.assertEqual(f.get_db_prep_lookup('exact', False, connection=connection), [False]) 107 159 self.assertEqual(f.get_db_prep_lookup('exact', '0', connection=connection), [False]) 108 160 self.assertEqual(f.get_db_prep_lookup('exact', 0, connection=connection), [False]) 109 self.assertEqual(f.get_db_prep_lookup('exact', None, connection=connection), [None])110 161 162 111 163 def _test_to_python(self, f): 112 164 self.assertTrue(f.to_python(1) is True) 113 165 self.assertTrue(f.to_python(0) is False) 114 166 115 167 def test_booleanfield_get_db_prep_lookup(self): 168 from django.db import connection 116 169 self._test_get_db_prep_lookup(models.BooleanField()) 170 self.assertRaises(ValidationError, models.BooleanField().get_db_prep_lookup, 'exact', None) 117 171 118 172 def test_nullbooleanfield_get_db_prep_lookup(self): 173 from django.db import connection 119 174 self._test_get_db_prep_lookup(models.NullBooleanField()) 175 self.assertEqual(models.NullBooleanField().get_db_prep_lookup('exact', None, connection=connection), [None]) 120 176 121 177 def test_booleanfield_to_python(self): 122 178 self._test_to_python(models.BooleanField())