diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
a
|
b
|
|
883 | 883 | 'invalid': _("'%s' value must be an integer."), |
884 | 884 | } |
885 | 885 | description = _("Integer") |
| 886 | MAX_INT = 2147483647 |
886 | 887 | |
887 | 888 | def get_prep_value(self, value): |
888 | 889 | if value is None: |
… |
… |
|
908 | 909 | raise exceptions.ValidationError(msg) |
909 | 910 | |
910 | 911 | 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} |
912 | 915 | defaults.update(kwargs) |
913 | 916 | return super(IntegerField, self).formfield(**defaults) |
914 | 917 | |
diff --git a/tests/regressiontests/model_forms_regress/models.py b/tests/regressiontests/model_forms_regress/models.py
a
|
b
|
|
69 | 69 | |
70 | 70 | class Meta: |
71 | 71 | unique_together = (('author', 'publication'), ('publication', 'edition'),) |
| 72 | |
| 73 | class TooLargeIntegers(models.Model): |
| 74 | integer = models.IntegerField(null=True, blank=True) |
| 75 | big_integer = models.BigIntegerField(null=True, blank=True) |
diff --git a/tests/regressiontests/model_forms_regress/tests.py b/tests/regressiontests/model_forms_regress/tests.py
a
|
b
|
|
9 | 9 | from django.test import TestCase |
10 | 10 | |
11 | 11 | from 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) |
13 | 14 | |
14 | 15 | |
15 | 16 | class ModelMultipleChoiceFieldTests(TestCase): |
… |
… |
|
490 | 491 | def test_modelform_factory_metaclass(self): |
491 | 492 | new_cls = modelform_factory(Person, form=CustomMetaclassForm) |
492 | 493 | self.assertEqual(new_cls.base_fields, {}) |
| 494 | |
| 495 | class 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) |