#36344: Accessing a related field after using defer() or only() unexpectedly
overwrites unsaved state on model instances
-------------------------------------+-------------------------------------
     Reporter:  Erick                |                     Type:
                                     |  Uncategorized
       Status:  new                  |                Component:  Database
                                     |  layer (models, ORM)
      Version:  5.2                  |                 Severity:  Normal
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
 When working with a model instance that was originally loaded with
 `only()` or `defer()`, accessing any related object not cached on the
 instance reverts unrelated fields to the value currently present in the
 database.

 This toy example reproduces the behavior:

 {{{
 class SomethingElse(Model):
     ...

 class Something(Model):
     foo = CharField(max_length=2)
     bar = CharField(max_length=2)
     other = ForeignKey(SomethingElse)


 Something.objects.create(
     foo="ab",
     bar="cd",
     other=SomethingElse.objects.create()
 )

 something = Something.objects.only("bar").first()
 something.foo = "ef"
 assert something.foo == "ef"

 something.other   # Accessing this deferred related field triggers the
 behavior
 assert something.foo == "ef"  # AssertionError
 }}}

 This is surprising behavior, because application code may interact with
 model instances without knowing how they were loaded from the database.
-- 
Ticket URL: <https://code.djangoproject.com/ticket/36344>
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 view this discussion visit 
https://groups.google.com/d/msgid/django-updates/010701965a7064de-efb2ae55-b125-478c-8b2d-d973e0068b14-000000%40eu-central-1.amazonses.com.

Reply via email to