Ticket #11716: 11716.patch

File 11716.patch, 2.9 KB (added by Leo Shklovskii, 15 years ago)

Patch against r11526

  • django/db/models/fields/__init__.py

     
    356356                _("This value must be an integer."))
    357357
    358358    def get_db_prep_value(self, value):
    359         if value is None:
    360             return None
    361         return int(value)
     359        return self.to_python(value)
    362360
    363361    def contribute_to_class(self, cls, name):
    364362        assert not cls._meta.has_auto_field, "A model can't have more than one AutoField."
     
    693691class IntegerField(Field):
    694692    empty_strings_allowed = False
    695693    def get_db_prep_value(self, value):
    696         if value is None:
    697             return None
    698         return int(value)
     694        return self.to_python(value)
    699695
    700696    def get_internal_type(self):
    701697        return "IntegerField"
  • tests/regressiontests/model_fields/tests.py

     
    2525            ImageFieldUsingFileTests, \
    2626            TwoImageFieldTests
    2727
    28 
     28class 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       
    2943class DecimalFieldTests(django.test.TestCase):
    3044    def test_to_python(self):
    3145        f = models.DecimalField(max_digits=4, decimal_places=2)
     
    7286        # This should not crash. That counts as a win for our purposes.
    7387        Foo.objects.filter(d__gte=100000000000)
    7488
     89class IntegerFieldTests(django.test.TestCase):
     90    def test_to_python(self):
     91        f = models.IntegerField()
     92        self.assertEqual(f.to_python(3), 3)
     93        self.assertEqual(f.to_python("3"), 3)
     94        self.assertEqual(f.to_python(None), None)
     95        self.assertRaises(ValidationError, f.to_python, "abc")
     96   
     97    def test_get_db_prep_value(self):
     98        f = models.IntegerField()
     99        self.assertEqual(f.get_db_prep_value(3), 3)
     100        self.assertEqual(f.get_db_prep_value("3"), 3)
     101        self.assertEqual(f.get_db_prep_value(None), None)
     102        self.assertRaises(ValidationError, f.get_db_prep_value, "abc")
     103
    75104class ForeignKeyTests(django.test.TestCase):
    76105    def test_callable_default(self):
    77106        """Test the use of a lazy callable for ForeignKey.default"""
Back to Top