Fixed #23801 -- Added warning when max_length is used with IntegerField
This commit is contained in:
parent
cbb5cdd155
commit
e9d1f1182a
|
@ -1704,6 +1704,23 @@ class IntegerField(Field):
|
|||
}
|
||||
description = _("Integer")
|
||||
|
||||
def check(self, **kwargs):
|
||||
errors = super(IntegerField, self).check(**kwargs)
|
||||
errors.extend(self._check_max_length_warning())
|
||||
return errors
|
||||
|
||||
def _check_max_length_warning(self):
|
||||
if self.max_length is not None:
|
||||
return [
|
||||
checks.Warning(
|
||||
"'max_length' is ignored when used with IntegerField",
|
||||
hint="Remove 'max_length' from field",
|
||||
obj=self,
|
||||
id='fields.W122',
|
||||
)
|
||||
]
|
||||
return []
|
||||
|
||||
@cached_property
|
||||
def validators(self):
|
||||
# These validators can't be added at field initialization time since
|
||||
|
|
|
@ -82,6 +82,7 @@ Fields
|
|||
* **fields.E110**: ``BooleanField``\s do not accept null values.
|
||||
* **fields.E120**: ``CharField``\s must define a ``max_length`` attribute.
|
||||
* **fields.E121**: ``max_length`` must be a positive integer.
|
||||
* **fields.W122**: ``max_length`` is ignored when used with ``IntegerField``.
|
||||
* **fields.E130**: ``DecimalField``\s must define a ``decimal_places`` attribute.
|
||||
* **fields.E131**: ``decimal_places`` must be a non-negative integer.
|
||||
* **fields.E132**: ``DecimalField``\s must define a ``max_digits`` attribute.
|
||||
|
|
|
@ -513,6 +513,25 @@ class ImageFieldTests(IsolatedModelsTestCase):
|
|||
self.assertEqual(errors, expected)
|
||||
|
||||
|
||||
class IntegerFieldTests(IsolatedModelsTestCase):
|
||||
|
||||
def test_max_length_warning(self):
|
||||
class Model(models.Model):
|
||||
value = models.IntegerField(max_length=2)
|
||||
|
||||
value = Model._meta.get_field('value')
|
||||
errors = Model.check()
|
||||
expected = [
|
||||
DjangoWarning(
|
||||
"'max_length' is ignored when used with IntegerField",
|
||||
hint="Remove 'max_length' from field",
|
||||
obj=value,
|
||||
id='fields.W122',
|
||||
)
|
||||
]
|
||||
self.assertEqual(errors, expected)
|
||||
|
||||
|
||||
class TimeFieldTests(IsolatedModelsTestCase):
|
||||
|
||||
def test_fix_default_value(self):
|
||||
|
|
Loading…
Reference in New Issue