summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAttila Tovt <uran198@gmail.com>2015-12-05 03:14:46 +0200
committerTim Graham <timograham@gmail.com>2015-12-05 17:23:13 -0500
commit6f229048ddd8c7347ff60dddfb9121e6021c7b2e (patch)
tree54f858e6b1f991bb1aa16f6c3421481fbd606d33
parentf5af68ba68c1041d785e5582529134d54895e7c6 (diff)
Fixed #25547 -- Made Model.refresh_from_db() update FileField's instance.
-rw-r--r--django/db/models/fields/files.py4
-rw-r--r--tests/model_fields/tests.py5
2 files changed, 9 insertions, 0 deletions
diff --git a/django/db/models/fields/files.py b/django/db/models/fields/files.py
index cc8fe181ba..3b39ba1f56 100644
--- a/django/db/models/fields/files.py
+++ b/django/db/models/fields/files.py
@@ -202,6 +202,10 @@ class FileDescriptor(object):
file.field = self.field
file.storage = self.field.storage
+ # Make sure that the instance is correct.
+ elif isinstance(file, FieldFile) and instance is not file.instance:
+ file.instance = instance
+
# That was fun, wasn't it?
return instance.__dict__[self.field.name]
diff --git a/tests/model_fields/tests.py b/tests/model_fields/tests.py
index 34793eacdb..77361fae79 100644
--- a/tests/model_fields/tests.py
+++ b/tests/model_fields/tests.py
@@ -792,6 +792,11 @@ class FileFieldTests(unittest.TestCase):
except OSError:
self.fail("Deleting an unset FileField should not raise OSError.")
+ def test_refresh_from_db(self):
+ d = Document.objects.create(myfile='something.txt')
+ d.refresh_from_db()
+ self.assertIs(d.myfile.instance, d)
+
class BinaryFieldTests(test.TestCase):
binary_data = b'\x00\x46\xFE'