#25547: refresh_from_db leaves FieldFile with reference to db_instance
-------------------------------------+-------------------------------------
     Reporter:  vinnyrose            |      Owner:  nobody
         Type:  Bug                  |     Status:  new
    Component:  Database layer       |    Version:  1.8
  (models, ORM)                      |   Keywords:  refresh_from_db,
     Severity:  Normal               |  fieldfile
 Triage Stage:  Unreviewed           |  Has patch:  0
Easy pickings:  0                    |      UI/UX:  0
-------------------------------------+-------------------------------------
 #### models.py
 {{{#!python
 class Product(models.Model):
     myfile = models.FileField(upload_to='folder')
 }}}

 ##### elsewhere.py
 {{{#!python
 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?

--
Ticket URL: <https://code.djangoproject.com/ticket/25547>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/052.907624e03515358f25a5ef7199041a0f%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to