Add get_object_cached_field() for fetching cached object field values.

Review Request #14326 — Created Feb. 4, 2025 and submitted — Latest diff uploaded

Information

Djblets
release-5.x

Reviewers

When working with a queryset involving select_related() or
prefetch_related(), it's often useful to be able to check for and
access data returned from the corresponding caches, without the risk of
triggering an unwanted lookup. We use this to conditionally access state
or perform a new query, depending on its presence.

To simplify this pattern, this change introduces
get_object_cached_field(), which returns cached state for a field or
relation on an object.

It first looks up in the field cache (which is populated by
select_related() and may also be populated by a field's implementation
manually).

If not found there, it then checks the prefetch_related() cache.

If still not found, it returns UNSET.

Unit tests pass.

Made use of this in in-progress work.

Diff Revision 3 (Latest)

orig
1
2
3

Commits

First Last Summary ID Author
Add get_object_cached_field() for fetching cached object field values.
When working with a queryset involving `select_related()` or `prefetch_related()`, it's often useful to be able to check for and access data returned from the corresponding caches, without the risk of triggering an unwanted lookup. We use this to conditionally access state or perform a new query, depending on its presence. To simplify this pattern, this change introduces `get_object_cached_field()`, which returns cached state for a field or relation on an object. It first looks up in the field cache (which is populated by `select_related()` and may also be populated by a field's implementation manually). If not found there, it then checks the `prefetch_related()` cache. If still not found, it returns `UNSET`.
fddb1307921c4d0e395937b0fded933f691eebe8 Christian Hammond
djblets/db/query.py
djblets/db/tests/test_query.py
Loading...