diff --git a/django/db/models/fields/files.py b/django/db/models/fields/files.py
index e0c0959..b0dce38 100644
a
|
b
|
class FileField(Field):
|
242 | 242 | # (ie. upload_to='path/to/upload/dir'), the length of the generated |
243 | 243 | # name equals the length of the uploaded name plus a constant. Thus |
244 | 244 | # we can tell the user how much shorter the name should be (roughly). |
245 | | length = len(self.generate_filename(model_instance, value.name)) |
| 245 | if value and value._committed: |
| 246 | filename = value.name |
| 247 | else: |
| 248 | filename = self.generate_filename(model_instance, value.name) |
| 249 | length = len(filename) |
246 | 250 | if self.max_length and length > self.max_length: |
247 | 251 | error_values = {'extra': length - self.max_length} |
248 | 252 | raise ValidationError(self.error_messages['max_length'] % error_values) |
diff --git a/tests/modeltests/files/models.py b/tests/modeltests/files/models.py
index 4134472..cefc7c7 100644
a
|
b
|
class Storage(models.Model):
|
26 | 26 | |
27 | 27 | normal = models.FileField(storage=temp_storage, upload_to='tests') |
28 | 28 | custom = models.FileField(storage=temp_storage, upload_to=custom_upload_to) |
29 | | random = models.FileField(storage=temp_storage, upload_to=random_upload_to) |
| 29 | random = models.FileField(storage=temp_storage, upload_to=random_upload_to, max_length=16) |
30 | 30 | default = models.FileField(storage=temp_storage, upload_to='tests', default='tests/default.txt') |
diff --git a/tests/modeltests/files/tests.py b/tests/modeltests/files/tests.py
index 3e256f7..eb81615 100644
a
|
b
|
class FileStorageTests(TestCase):
|
102 | 102 | obj4.random.save("random_file", ContentFile(b"random content")) |
103 | 103 | self.assertTrue(obj4.random.name.endswith("/random_file")) |
104 | 104 | |
| 105 | # Ticket #18515: validation for an already saved file should not check |
| 106 | # against a regenerated file name (and potentially raise a ValidationError |
| 107 | # if max_length is exceeded) |
| 108 | obj4.full_clean() |
| 109 | |
105 | 110 | # Clean up the temporary files and dir. |
106 | 111 | obj1.normal.delete() |
107 | 112 | obj2.normal.delete() |