Opened 7 years ago

Closed 7 years ago

#25547 closed Bug (fixed)

refresh_from_db leaves FieldFile with reference to db_instance

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



class Product(models.Model):
    myfile = models.FileField(upload_to='folder')


product = Product.objects.create(myfile='filename')

assert id(product.myfile.instance) == id(product) # false

product.file.instance is actually db_instance which is used internally in refresh_from_db.

This leads to strange results when trying to use or FieldFile.delete, since you would be modifying db_instance instead of the intended instance. This also means any field that uses a descriptor_class that returns an attr_class will have this same issue, I've used this pattern for custom fields.

This could most simply be fixed in the FileDescriptor itself, because frankly I don't see how this could be fixed for all cases in which a field has reference to the instance from which it was instatianted from. Perhaps a warning on refresh_from_db in the docs about this risk?

Change History (4)

comment:1 Changed 7 years ago by Tim Graham

Triage Stage: UnreviewedAccepted

comment:2 Changed 7 years ago by Attila Tovt

comment:3 Changed 7 years ago by Attila Tovt

Has patch: set

comment:4 Changed 7 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: newclosed

In 6f229048:

Fixed #25547 -- Made Model.refresh_from_db() update FileField's instance.

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