Opened 3 years ago

Last modified 3 years ago

#18283 new Bug

FileField should not reuse FieldFiles

Reported by: sayane Owned by: nobody
Component: Database layer (models, ORM) Version: 1.4
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

A simple example:

old_image = mymodel.image
old_image.open()
try:
    mymodel.image = do_something_with_image(old_image)
finally:
    old_image.close()

mymodel.save() # transaction manager is in auto commit mode
old_image.delete()

Expected behavior:

New file is commited to disk and database, old file is removed.

Actual behavior:

New file is commited to disk and database, then this new file is removed from disk.

That's because FileField (or to be more precise: FileDescriptor class) is re-using FieldFile objects instead of creating new FieldFile for every new instance of a file. This leads to unexpected results and data loss (like in example).

Change History (1)

comment:1 Changed 3 years ago by jezdez

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted
Note: See TracTickets for help on using tickets.
Back to Top