﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
26469	FieldFile.open() does not properly set mode when opening file	Nathan Osman	nobody	"Consider the following model definition:

{{{
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."	Bug	closed	Database layer (models, ORM)	1.9	Normal	duplicate			Unreviewed	0	0	0	0	0	0
