summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--django/db/models/fields/files.py6
-rw-r--r--tests/model_fields/test_imagefield.py7
2 files changed, 11 insertions, 2 deletions
diff --git a/django/db/models/fields/files.py b/django/db/models/fields/files.py
index e10c8cd69a..95985684ee 100644
--- a/django/db/models/fields/files.py
+++ b/django/db/models/fields/files.py
@@ -458,8 +458,10 @@ class ImageField(FileField):
Dimensions can be forced to update with force=True, which is how
ImageFileDescriptor.__set__ calls this method.
"""
- # Nothing to update if the field is deferred.
- if self.attname not in instance.__dict__:
+ # Nothing to update if the field doesn't have dimension fields or if
+ # the field is deferred.
+ has_dimension_fields = self.width_field or self.height_field
+ if not has_dimension_fields or self.attname not in instance.__dict__:
return
# getattr will call the ImageFileDescriptor's __get__ method, which
diff --git a/tests/model_fields/test_imagefield.py b/tests/model_fields/test_imagefield.py
index 81ac9dad61..8bbfee30f2 100644
--- a/tests/model_fields/test_imagefield.py
+++ b/tests/model_fields/test_imagefield.py
@@ -336,6 +336,13 @@ class ImageFieldNoDimensionsTests(ImageFieldTwoDimensionsTests):
[sender_id for (_, sender_id), *_ in signals.post_init.receivers],
)
+ def test_save_does_not_close_file(self):
+ p = self.PersonModel(name="Joe")
+ p.mugshot.save("mug", self.file1)
+ with p.mugshot as f:
+ # Underlying file object wasn’t closed.
+ self.assertEqual(f.tell(), 0)
+
@skipIf(Image is None, "Pillow is required to test ImageField")
class ImageFieldOneDimensionTests(ImageFieldTwoDimensionsTests):