Refs #23801 -- Made integer field max_length warning show correct field type.

This commit is contained in:
Nick Pope 2017-12-11 15:35:19 +00:00 committed by Tim Graham
parent 856ba1ec86
commit c512912463
3 changed files with 20 additions and 12 deletions

View File

@ -1773,7 +1773,7 @@ class IntegerField(Field):
if self.max_length is not None:
return [
checks.Warning(
"'max_length' is ignored when used with IntegerField",
"'max_length' is ignored when used with %s." % self.__class__.__name__,
hint="Remove 'max_length' from field",
obj=self,
id='fields.W122',

View File

@ -143,7 +143,8 @@ Model fields
appeared before support for null values was added in Django 2.1.*
* **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.W122**: ``max_length`` is ignored when used with
``<integer field type>``.
* **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.

View File

@ -617,12 +617,19 @@ class IntegerFieldTests(SimpleTestCase):
def test_max_length_warning(self):
class Model(models.Model):
value = models.IntegerField(max_length=2)
integer = models.IntegerField(max_length=2)
biginteger = models.BigIntegerField(max_length=2)
smallinteger = models.SmallIntegerField(max_length=2)
positiveinteger = models.PositiveIntegerField(max_length=2)
positivesmallinteger = models.PositiveSmallIntegerField(max_length=2)
field = Model._meta.get_field('value')
for field in Model._meta.get_fields():
if field.auto_created:
continue
with self.subTest(name=field.name):
self.assertEqual(field.check(), [
DjangoWarning(
"'max_length' is ignored when used with IntegerField",
"'max_length' is ignored when used with %s." % field.__class__.__name__,
hint="Remove 'max_length' from field",
obj=field,
id='fields.W122',