Opened 16 years ago

Closed 16 years ago

Last modified 13 years ago

#8534 closed (fixed)

Querying for FileField.size leaves file open

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

Description

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.

Attachments (2)

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

Download all attachments as: .zip

Change History (7)

by snaury, 16 years ago

by snaury, 16 years ago

Attachment: myproject.tar.bz2 added

Sample project that shows the problem

comment:1 by snaury, 16 years ago

Cc: snaury@… added

comment:2 by snaury, 16 years ago

milestone: 1.0

comment:3 by Jacob, 16 years ago

Triage Stage: UnreviewedAccepted

comment:4 by Jacob, 16 years ago

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 by Jacob, 13 years ago

milestone: 1.0

Milestone 1.0 deleted

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