Opened 10 years ago
Closed 10 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 |
Description
#### models.py
class Product(models.Model): myfile = models.FileField(upload_to='folder')
##### elsewhere.py
product = Product.objects.create(myfile='filename') product.refresh_from_db() 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 FieldFile.save 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 by , 10 years ago
| Triage Stage: | Unreviewed → Accepted |
|---|
comment:2 by , 10 years ago
comment:3 by , 10 years ago
| Has patch: | set |
|---|
PR