summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Sinchok <chris@sinchok.com>2016-07-19 15:17:39 -0500
committerTim Graham <timograham@gmail.com>2016-08-09 12:53:18 -0400
commitac1975b18b5a33234284bec86e5a5bb44a4af976 (patch)
tree96e5d955b8dc47f8c2df7dab7c701f10f6c89de3
parentade681b9ad2a97833cd3f06530fba01e51250b32 (diff)
Fixed #13809 -- Made FieldFile.open() respect its mode argument.
-rw-r--r--django/db/models/fields/files.py5
-rw-r--r--tests/file_storage/tests.py10
2 files changed, 14 insertions, 1 deletions
diff --git a/django/db/models/fields/files.py b/django/db/models/fields/files.py
index 659bb4c518..715b5527f8 100644
--- a/django/db/models/fields/files.py
+++ b/django/db/models/fields/files.py
@@ -79,7 +79,10 @@ class FieldFile(File):
def open(self, mode='rb'):
self._require_file()
- self.file.open(mode)
+ if hasattr(self, '_file') and self._file is not None:
+ self.file.open(mode)
+ else:
+ self.file = self.storage.open(self.name, mode)
# open() doesn't alter the file's contents, but it does reset the pointer
open.alters_data = True
diff --git a/tests/file_storage/tests.py b/tests/file_storage/tests.py
index e8425558e8..acb663ef7a 100644
--- a/tests/file_storage/tests.py
+++ b/tests/file_storage/tests.py
@@ -742,6 +742,16 @@ class FileFieldStorageTests(TestCase):
self.assertEqual(list(obj.normal.chunks(chunk_size=2)), [b"co", b"nt", b"en", b"t"])
obj.normal.close()
+ def test_filefield_write(self):
+ # Files can be written to.
+ obj = Storage.objects.create(normal=SimpleUploadedFile('rewritten.txt', b'content'))
+ with obj.normal as normal:
+ normal.open('wb')
+ normal.write(b'updated')
+ obj.refresh_from_db()
+ self.assertEqual(obj.normal.read(), b'updated')
+ obj.normal.close()
+
def test_filefield_reopen(self):
obj = Storage.objects.create(normal=SimpleUploadedFile('reopen.txt', b'content'))
with obj.normal as normal: