﻿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
8534	Querying for FileField.size leaves file open	snaury	nobody	"I have the following model in my application:

{{{
class ProjectFile(models.Model):
    data = models.FileField(upload_to='files')

    def __unicode__(self):
        return u""%s (%d bytes)"" % (self.data.name, self.data.size)
}}}

As you can see its {{{__unicode__}}} method queries for !FileField.size and returns a string showing file size along with file name. The problem is when I navigate to [http://localhost:8000/admin/main/projectfile/ /admin/main/projectfile/] and everytime I refresh it opens a new file and they all stay open. Judging from the fact that they are not closed for a very long time (however, if I refresh it for something like 10 times, some old files can get closed, but far from all) it seems as if admin interface is caching model instances somewhere, thus leaving files open.

On Windows it has another side effect: you cannot delete open files, thus you cannot delete entries with models like this via admin interface.

The patch is very simple, we should override !FieldFile.size to use self.storage.size, however I can't come up with any test for this. While on Windows we can test if the file is still open by trying to delete it, I have no clue how to do it on Unix."		closed	File uploads/storage	dev		fixed		snaury@…	Accepted	1	0	0	0	0	0
