diff --git a/django/db/models/fields/files.py b/django/db/models/fields/files.py
index 61a903e..aefe388 100644
a
|
b
|
class ImageFileDescriptor(FileDescriptor):
|
300 | 300 | assigning the width/height to the width_field/height_field, if appropriate. |
301 | 301 | """ |
302 | 302 | def __set__(self, instance, value): |
| 303 | update_dimensions = instance.__dict__.get(self.field.name) is not None |
303 | 304 | super(ImageFileDescriptor, self).__set__(instance, value) |
304 | 305 | |
305 | 306 | # The rest of this method deals with width/height fields, so we can |
306 | | # bail early if neither is used. |
307 | | if not self.field.width_field and not self.field.height_field: |
| 307 | # bail early if neither is used or we don't have to update the |
| 308 | # dimensons because the data is coming from the database |
| 309 | if not (update_dimensions and (self.field.width_field or |
| 310 | self.field.height_field)): |
308 | 311 | return |
309 | 312 | |
310 | 313 | # We need to call the descriptor's __get__ to coerce this assigned |
diff --git a/tests/regressiontests/file_storage/models.py b/tests/regressiontests/file_storage/models.py
index 32af5d7..b41c924 100644
a
|
b
|
import os
|
2 | 2 | import tempfile |
3 | 3 | import shutil |
4 | 4 | from django.db import models |
| 5 | from django.db.models.fields.files import ImageFieldFile, ImageField |
5 | 6 | from django.core.files.storage import FileSystemStorage |
6 | 7 | from django.core.files.base import ContentFile |
7 | 8 | |
… |
… |
except ImportError:
|
17 | 18 | |
18 | 19 | # If we have PIL, do these tests |
19 | 20 | if Image: |
| 21 | class TestImageFieldFile(ImageFieldFile): |
| 22 | def __init__(self, *args, **kwargs): |
| 23 | self.test_was_opened = False |
| 24 | super(TestImageFieldFile, self).__init__(*args,**kwargs) |
| 25 | def open(self): |
| 26 | self.test_was_opened = True |
| 27 | super(TestImageFieldFile, self).open() |
| 28 | class TestImageField(ImageField): |
| 29 | attr_class = TestImageFieldFile |
| 30 | |
20 | 31 | temp_storage_dir = tempfile.mkdtemp() |
21 | 32 | temp_storage = FileSystemStorage(temp_storage_dir) |
22 | 33 | |
23 | 34 | class Person(models.Model): |
24 | 35 | name = models.CharField(max_length=50) |
25 | | mugshot = models.ImageField(storage=temp_storage, upload_to='tests', |
| 36 | mugshot = TestImageField(storage=temp_storage, upload_to='tests', |
26 | 37 | height_field='mug_height', |
27 | 38 | width_field='mug_width') |
28 | 39 | mug_height = models.PositiveSmallIntegerField() |
… |
… |
True
|
72 | 83 | |
73 | 84 | # Get a "clean" model instance |
74 | 85 | >>> p3 = Person.objects.get(name="Joan") |
| 86 | >>> p3.mugshot.test_was_opened |
| 87 | False |
75 | 88 | |
76 | 89 | # It won't have an opened file. |
77 | 90 | >>> p3.mugshot.closed |
78 | 91 | True |
79 | 92 | |
| 93 | # Bug #11084: If the file is unavailable, still create the object without error. |
| 94 | >>> path = p3.mugshot.path |
| 95 | >>> shutil.move(path, path + '2') |
| 96 | >>> p4 = Person.objects.get(name="Joan") |
| 97 | >>> shutil.move(path + '2', path) |
| 98 | |
| 99 | |
80 | 100 | # After asking for the size, the file should still be closed. |
81 | 101 | >>> _ = p3.mugshot.size |
82 | 102 | >>> p3.mugshot.closed |