diff --git a/django/db/models/fields/files.py b/django/db/models/fields/files.py index 9d4a06daca..0b2a24a895 100644 --- a/django/db/models/fields/files.py +++ b/django/db/models/fields/files.py @@ -270,9 +270,7 @@ class FileField(Field): def deconstruct(self): name, path, args, kwargs = super(FileField, self).deconstruct() - if kwargs.get("max_length", None) != 100: - kwargs["max_length"] = 100 - else: + if kwargs.get("max_length", None) == 100: del kwargs["max_length"] kwargs['upload_to'] = self.upload_to if self.storage is not default_storage: diff --git a/tests/field_deconstruction/tests.py b/tests/field_deconstruction/tests.py index 3fea7ae853..0d24c2a268 100644 --- a/tests/field_deconstruction/tests.py +++ b/tests/field_deconstruction/tests.py @@ -128,6 +128,11 @@ class FieldDeconstructionTests(TestCase): self.assertEqual(path, "django.db.models.FileField") self.assertEqual(args, []) self.assertEqual(kwargs, {"upload_to": "foo/bar"}) + field = models.FileField(upload_to="foo/bar", max_length=200) + name, path, args, kwargs = field.deconstruct() + self.assertEqual(path, "django.db.models.FileField") + self.assertEqual(args, []) + self.assertEqual(kwargs, {"upload_to": "foo/bar", "max_length": 200}) def test_file_path_field(self): field = models.FilePathField(match=".*\.txt$") @@ -135,11 +140,11 @@ class FieldDeconstructionTests(TestCase): self.assertEqual(path, "django.db.models.FilePathField") self.assertEqual(args, []) self.assertEqual(kwargs, {"match": ".*\.txt$"}) - field = models.FilePathField(recursive=True, allow_folders=True) + field = models.FilePathField(recursive=True, allow_folders=True, max_length=123) name, path, args, kwargs = field.deconstruct() self.assertEqual(path, "django.db.models.FilePathField") self.assertEqual(args, []) - self.assertEqual(kwargs, {"recursive": True, "allow_folders": True}) + self.assertEqual(kwargs, {"recursive": True, "allow_folders": True, "max_length": 123}) def test_float_field(self): field = models.FloatField() @@ -276,11 +281,11 @@ class FieldDeconstructionTests(TestCase): self.assertEqual(path, "django.db.models.SlugField") self.assertEqual(args, []) self.assertEqual(kwargs, {}) - field = models.SlugField(db_index=False) + field = models.SlugField(db_index=False, max_length=231) name, path, args, kwargs = field.deconstruct() self.assertEqual(path, "django.db.models.SlugField") self.assertEqual(args, []) - self.assertEqual(kwargs, {"db_index": False}) + self.assertEqual(kwargs, {"db_index": False, "max_length": 231}) def test_small_integer_field(self): field = models.SmallIntegerField() @@ -302,3 +307,9 @@ class FieldDeconstructionTests(TestCase): self.assertEqual(path, "django.db.models.URLField") self.assertEqual(args, []) self.assertEqual(kwargs, {}) + field = models.URLField(max_length=231) + name, path, args, kwargs = field.deconstruct() + self.assertEqual(path, "django.db.models.URLField") + self.assertEqual(args, []) + self.assertEqual(kwargs, {"max_length": 231}) +