Add get_object_cached_field() for fetching cached object field values.

Review Request #14326 — Created Feb. 4, 2025 and updated

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.

Summary ID
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
Description From Last Updated

local variable 'user1' is assigned to but never used Column: 9 Error code: F841

reviewbotreviewbot

typo: catched -> cached

daviddavid
Checks run (1 failed, 1 succeeded)
flake8 failed.
JSHint passed.

flake8

chipx86
david
  1. 
      
  2. djblets/db/query.py (Diff revision 2)
     
     
    Show all issues

    typo: catched -> cached

  3. 
      
chipx86
Review request changed
Change Summary:

Fixed a typo in a comment.

Commits:
Summary ID
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`.
183a279e9b421dfa211b04b2354e65dcaf20ceaa
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

Checks run (2 succeeded)

flake8 passed.
JSHint passed.
david
  1. Ship It!
  2. 
      
maubin
  1. Ship It!
  2.