Code

Opened 6 years ago

Closed 6 years ago

Last modified 3 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:

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 6 years ago.
myproject.tar.bz2 (2.3 KB) - added by snaury 6 years ago.
Sample project that shows the problem

Download all attachments as: .zip

Change History (7)

Changed 6 years ago by snaury

Changed 6 years ago by snaury

Sample project that shows the problem

comment:1 Changed 6 years ago by snaury

  • Cc snaury@… added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 6 years ago by snaury

  • milestone set to 1.0

comment:3 Changed 6 years ago by jacob

  • Triage Stage changed from Unreviewed to Accepted

comment:4 Changed 6 years ago by jacob

  • Resolution set to fixed
  • Status changed from new to closed

(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 3 years ago by jacob

  • milestone 1.0 deleted

Milestone 1.0 deleted

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.