diff options
| author | Alexey Kotlyarov <a@koterpillar.com> | 2016-03-23 09:33:35 +1100 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2016-03-23 10:05:26 -0400 |
| commit | a52a531a8b34f049fba11c3ee7b010af7534bf90 (patch) | |
| tree | e309092c91da1540d18e0e72c53eda47a82a085c | |
| parent | 9390da7fb6e251eaa9a785692f987296cb14523f (diff) | |
Fixed #26398 -- Made FieldFile.open() respect its mode argument.
| -rw-r--r-- | django/db/models/fields/files.py | 7 | ||||
| -rw-r--r-- | tests/file_storage/tests.py | 10 |
2 files changed, 13 insertions, 4 deletions
diff --git a/django/db/models/fields/files.py b/django/db/models/fields/files.py index 469fe60f28..a222e491af 100644 --- a/django/db/models/fields/files.py +++ b/django/db/models/fields/files.py @@ -42,10 +42,10 @@ class FieldFile(File): if not self: raise ValueError("The '%s' attribute has no file associated with it." % self.field.name) - def _get_file(self): + def _get_file(self, mode='rb'): self._require_file() if not hasattr(self, '_file') or self._file is None: - self._file = self.storage.open(self.name, 'rb') + self._file = self.storage.open(self.name, mode) return self._file def _set_file(self, file): @@ -74,8 +74,7 @@ class FieldFile(File): size = property(_get_size) def open(self, mode='rb'): - self._require_file() - self.file.open(mode) + self._get_file(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 5583b2df9c..7257f29b29 100644 --- a/tests/file_storage/tests.py +++ b/tests/file_storage/tests.py @@ -710,6 +710,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_duplicate_filename(self): # Multiple files with the same name get _(7 random chars) appended to them. objs = [Storage() for i in range(2)] |
