#21584: prefetch_related child queryset does not update on create
-------------------------------------+-------------------------------------
Reporter: lucaswiman | Owner: nobody
Type: Uncategorized | Status: closed
Component: Database layer | Version: 1.6
(models, ORM) | Resolution: invalid
Severity: Normal | Triage Stage:
Keywords: | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by lukeplant):
Sorry, that's just how the ORM works. Objects that represent collections
do not keep track of their elements, because they represent querysets i.e.
queries that may or may not yet have been evaluated, not actual
collections of objects. If you have:
{{{
#!python
my_objects = Foo.objects.all().filter(bar=1)
list(my_objects) # evaluate query
my_objects.update(bar=2)
}}}
then you will find that the 'update' has not affected anything in
`my_objects` - either by changing the instances, or by removing them from
the collection (since they no longer match the requirement `bar=1`).
In the same way, `p.child_set` does not keep track of elements that are
referred to. When you call `all()`, it executes a query every time,
(rather than tracking creates/adds/deletes etc.). If you have used
`prefetch_related`, however, it never executes a query when you just do
`all()` because it has been prefetched. This is exactly what
`prefetch_related` is supposed to do - the `all()` will not return data to
reflect what is in the DB at that moment in time, but what was in the DB
when the query was first evaluated.
--
Ticket URL: <https://code.djangoproject.com/ticket/21584#comment:3>
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/068.0ef5524914886e6eba90bc7203940889%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.