Ticket #16747: too-large-integers.diff

File too-large-integers.diff, 3.2 KB (added by Petr Marhoun <petr.marhoun@…>, 13 years ago)
  • django/db/models/fields/__init__.py

    diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
    a b  
    883883        'invalid': _("'%s' value must be an integer."),
    884884    }
    885885    description = _("Integer")
     886    MAX_INT = 2147483647
    886887
    887888    def get_prep_value(self, value):
    888889        if value is None:
     
    908909            raise exceptions.ValidationError(msg)
    909910
    910911    def formfield(self, **kwargs):
    911         defaults = {'form_class': forms.IntegerField}
     912        defaults = {'form_class': forms.IntegerField,
     913                    'min_value': -IntegerField.MAX_INT - 1,
     914                    'max_value': IntegerField.MAX_INT}
    912915        defaults.update(kwargs)
    913916        return super(IntegerField, self).formfield(**defaults)
    914917
  • tests/regressiontests/model_forms_regress/models.py

    diff --git a/tests/regressiontests/model_forms_regress/models.py b/tests/regressiontests/model_forms_regress/models.py
    a b  
    6969
    7070    class Meta:
    7171        unique_together = (('author', 'publication'), ('publication', 'edition'),)
     72
     73class TooLargeIntegers(models.Model):
     74    integer = models.IntegerField(null=True, blank=True)
     75    big_integer = models.BigIntegerField(null=True, blank=True)
  • tests/regressiontests/model_forms_regress/tests.py

    diff --git a/tests/regressiontests/model_forms_regress/tests.py b/tests/regressiontests/model_forms_regress/tests.py
    a b  
    99from django.test import TestCase
    1010
    1111from models import (Person, RealPerson, Triple, FilePathModel, Article,
    12     Publication, CustomFF, Author, Author1, Homepage, Document, Edition)
     12    Publication, CustomFF, Author, Author1, Homepage, Document, Edition,
     13    TooLargeIntegers)
    1314
    1415
    1516class ModelMultipleChoiceFieldTests(TestCase):
     
    490491    def test_modelform_factory_metaclass(self):
    491492        new_cls = modelform_factory(Person, form=CustomMetaclassForm)
    492493        self.assertEqual(new_cls.base_fields, {})
     494
     495class TooLargeIntegersTestCase(TestCase):
     496
     497    def test_integers(self):
     498        for sign in (-1, 1):
     499            for diff in (-1, 0, 1):
     500                value = sign * (2 ** 31 + diff)
     501                form = modelform_factory(TooLargeIntegers)({'integer': value})
     502                if form.is_valid():
     503                    pk = form.save().pk
     504                    self.assertEqual(TooLargeIntegers.objects.get(pk=pk).integer, value)
     505        self.assertEqual(TooLargeIntegers.objects.count(), 3)
     506        self.assertEqual(TooLargeIntegers.objects.filter(integer__gt=0).count(), 1)
     507
     508    def test_big_integers(self):
     509        for sign in (-1, 1):
     510            for diff in (-1, 0, 1):
     511                value = sign * (2 ** 63 + diff)
     512                form = modelform_factory(TooLargeIntegers)({'big_integer': value})
     513                if form.is_valid():
     514                    pk = form.save().pk
     515                    self.assertEqual(TooLargeIntegers.objects.get(pk=pk).big_integer, value)
     516        self.assertEqual(TooLargeIntegers.objects.count(), 3)
     517        self.assertEqual(TooLargeIntegers.objects.filter(big_integer__gt=0).count(), 1)
Back to Top