#37027: refresh_from_db() with from_queryset + prefetch does not persist result
in
instance
-------------------------------------+-------------------------------------
Reporter: Hugo Maingonnat | Owner: (none)
Type: Bug | Status: closed
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: refresh_from_db, | Triage Stage:
prefetch | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Hugo Maingonnat):
Thank you both for your answers ! If I may give a bit more context. I
understand this is only for single instances, but end users are always
happy if CRUD endpoints are faster (especially if called multiple times,
with multiple nested objects).
- Using `foo.bar_set.all()` will not necessarily fill up nested objects
(and `to_attr`), and each level will trigger lazy queries.
- My instance has initially been created with both `select_related` and
`prefetch_related` via a queryset in a drf view. From a user point of
view, I can do like below but the API feels a bit inconsistent. Especially
users would not expect the current silent behavior if they try to set a
prefetch in the `from_queryset` and see that it is not working.
{{{
foo.refresh_from_db(from_queryset=Foo.objects.select_related('baz'))
prefetch = Prefetch('bar_set', queryset=Bar.objects.all())
prefetch_related_objects([foo], prefetch)
}}}
- Thank you @Jacob for the workaround. I am not entirely sure to
understand why using `_avoid_cloning()` make it works. I feel if it works
with `_avoid_cloning()`, it could just work with the clone API ?
Another suggestion would be to have something simpler
`refresh_from_db(select_related=True, prefetch_related=True)` (or another
method name like `deep_refresh_from_db()`), that just refresh all
relations that the initial object had. But I am not sure if it still has a
reference to the initial queryset.
--
Ticket URL: <https://code.djangoproject.com/ticket/37027#comment:4>
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/0107019d8ce81941-501d66ab-2968-4c9f-8323-5ea4ac9bc03e-000000%40eu-central-1.amazonses.com.