• 
      

    Rebuild cached model relations when installing extension apps.

    Review Request #15219 — Created Aug. 7, 2026 and updated — Latest diff uploaded

    Information

    Djblets
    release-6.x

    Reviewers

    We currently have a bug where a model's relation tree can be cached before
    an extension providing a related model is registered, leaving the relation
    permanently missing from it.

    Django's Apps.set_installed_apps() sets Apps.ready to False before both
    of its calls to Apps.clear_cache() (its own, and the one at the end of
    Apps.populate()). However clear_cache() only expires the per-model
    Options caches when the registry is ready. Installing an app therefore
    never rebuilds those cached relation trees.

    This bug is exposed when an extension imports something from another extension
    at module scope. For example, an extension that uses Power Pack's User Roles
    in an approval hook will likely import the user role model at the module scope
    (we do this in an example in Review Board's documentation). The extension
    manager imports every extension's module before initializing any of them, so
    in this case the related model gets registered but is unreachable through
    AppConfig. Then if something accesses the model's relation tree before the
    extensions are registered, the tree is cached without the extension model's
    relation. This was bug was discovered when trying to import User Roles in
    RBCommons' team admin code.

    This change makes ExtensionManager._add_to_installed_apps() clear the cache
    itself, so that model relations can be properly populated.

    • Ran unit tests.
    • Saw the FieldError I was getting in RBCommons from the User Roles
      relation being missing go away.

    Commits

    Files