• 
      

    Add get_object_cached_field() for fetching cached object field values.

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

    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
    david
    1. Ship It!
    2. 
        
    maubin
    1. Ship It!
    2. 
        
    chipx86
    Review request changed
    Status:
    Completed
    Change Summary:
    Pushed to release-5.x (f6d0171)