Querying for FileField.size leaves file open
|Reported by:||snaury||Owned by:||nobody|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
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 /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.
Change History (7)
comment:1 Changed 8 years ago by
|Patch needs improvement:||unset|