{{{
class Thing(models.Model):
file = models.FileField(upload_to="things")
}}}
Let's suppose I have an existing model instance and I want to open the
file for writing:
{{{
t = Thing.objects.get(pk=1)
t.file.open('wb')
t.file.write(data)
}}}
This raises an unexpected exception:
{{{
IOError: File not open for writing
}}}
Here's the source code for `FieldFile.open()`:
{{{
def open(self, mode='rb'):
self._require_file()
self.file.open(mode)
}}}
The second line in the body accesses `self.file` - a property which is
defined as follows:
{{{
def _get_file(self):
self._require_file()
if not hasattr(self, '_file') or self._file is None:
self._file = self.storage.open(self.name, 'rb')
return self._file
# ...
file = property(_get_file, ...
}}}
If `self._file` is `None` (which is the case in my example above), the
storage backend is then instructed to open the file with mode 'rb' instead
of the mode that was originally passed to `FieldFile.open()`. The newly
opened `File` instance is returned and control resumes in
`FieldFile.open()` which invokes the `open()` method on the `File`
instance.
Here are the first few lines of `File.open()`:
{{{
def open(self, mode=None):
if not self.closed:
self.seek(0)
elif self.name and os.path.exists(self.name):
self.file = open(self.name, mode or self.mode)
# ...
}}}
Since the file is already open, `not self.closed` evaluates to `True` and
the file remains open in read-only mode. It is not possible to write to
the file.
--
Ticket URL: <https://code.djangoproject.com/ticket/26469>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* status: new => closed
* needs_docs: => 0
* resolution: => duplicate
* needs_tests: => 0
* needs_better_patch: => 0
Comment:
Duplicate of #26398. This should be fixed in master.
--
Ticket URL: <https://code.djangoproject.com/ticket/26469#comment:1>