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 1

This is not the most recent revision of the diff. The latest diff is revision 3. See what's changed.

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`.
4e7a65ed9bf2da9ed65369836dd567352f01ff1e Christian Hammond
djblets/db/query.py
djblets/db/tests/test_query.py
Loading...