Opened 10 years ago

Closed 10 years ago

Last modified 7 years ago

#8534 closed (fixed)

Querying for FileField.size leaves file open

Reported by: snaury Owned by: nobody
Component: File uploads/storage Version: master
Severity: Keywords:
Cc: snaury@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


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)" % (,

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, 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.

Attachments (2)

django-8534-filefield-size.patch (512 bytes) - added by snaury 10 years ago.
myproject.tar.bz2 (2.3 KB) - added by snaury 10 years ago.
Sample project that shows the problem

Download all attachments as: .zip

Change History (7)

Changed 10 years ago by snaury

Changed 10 years ago by snaury

Attachment: myproject.tar.bz2 added

Sample project that shows the problem

comment:1 Changed 10 years ago by snaury

Cc: snaury@… added

comment:2 Changed 10 years ago by snaury

milestone: 1.0

comment:3 Changed 10 years ago by Jacob

Triage Stage: UnreviewedAccepted

comment:4 Changed 10 years ago by Jacob

Resolution: fixed
Status: newclosed

(In [8638]) Fixed #8534: getting the size of a file no longer opens it (at least for the built-in file-system storage). Thanks, snaury.

comment:5 Changed 7 years ago by Jacob

milestone: 1.0

Milestone 1.0 deleted

Note: See TracTickets for help on using tickets.
Back to Top