:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(admin): build the admin URL conf from the full lazy registry (#101717)
Julian Bez5420667f46ac
diff --git a/ee/urls.py b/ee/urls.pyindex 45e6e07b4af8..34852a0a5647 100644--- a/ee/urls.py+++ b/ee/urls.py@@ -10,6 +10,7 @@ from django.views.generic import RedirectViewfrom django_otp.plugins.otp_static.models import StaticDevicefrom django_otp.plugins.otp_totp.models import TOTPDevice+from posthog.admin import register_all_adminfrom posthog.middleware import impersonated_session_logoutfrom posthog.views import api_key_search_view, redis_edit_ttl_view, redis_values_view@@ -74,6 +75,11 @@ def extend_api_router() -> None:# The admin interface is disabled on self-hosted instances, as its misuse can be unsafeif settings.ADMIN_PORTAL_ENABLED:+ # `AdminSite.get_urls()` derives the `admin:app_list` URL pattern from the registry+ # when `admin.site.urls` below is read, and never rebuilds it. `LazyAdminRegistry`+ # fills the registry first, but `posthog/apps.py` skips it under `settings.TEST`.+ register_all_admin()+# these models are auto-registered but we don't want to expose them to stafffor model in (StaticDevice, TOTPDevice):try:diff --git a/posthog/admin/__init__.py b/posthog/admin/__init__.pyindex 784cd2dfcc5e..6fbcf6267da5 100644--- a/posthog/admin/__init__.py+++ b/posthog/admin/__init__.py@@ -1,16 +1,28 @@# Lazy load admin classes to avoid loading all at startup.# Admin classes are loaded when Django admin site is first accessed+_registered_all_admin = False+def register_all_admin():"""Trigger every admin registration. Called lazily on first- `admin.site._registry` access via `LazyAdminRegistry`.+ `admin.site._registry` access via `LazyAdminRegistry`, and directly from the+ admin URL conf in `ee/urls.py`. Runs its body at most once per process.`INSTALLED_APPS` uses `SimpleAdminConfig` so Django doesn't autodiscover at`django.setup()` — we run the same primitive ourselves here, deferred. Thatkeeps every admin module out of `django.setup()` and out of every shell,worker, and management command that doesn't touch the admin."""+ global _registered_all_admin+ if _registered_all_admin:+ return+ # The guard is set before the body, not after it. `django.contrib.auth.admin`+ # registers `Group` while it is still importing, which re-enters here through+ # `LazyAdminRegistry`. The early return lets `auth.admin` finish, so+ # `admins/user_admin.py` can take `UserAdmin` from a complete module below.+ _registered_all_admin = True+from django.contrib import adminfrom django.utils.module_loading import autodiscover_modulesdiff --git a/posthog/settings/web.py b/posthog/settings/web.pyindex 2c45a7235fe8..3ed94d47d4a0 100644--- a/posthog/settings/web.py+++ b/posthog/settings/web.py@@ -122,9 +122,9 @@ PRODUCTS_APPS = [INSTALLED_APPS = ["whitenoise.runserver_nostatic", # makes sure that whitenoise handles static files in development# `SimpleAdminConfig` skips Django's eager `autodiscover_modules('admin')` at- # startup. We invoke autodiscover ourselves from `register_all_admin()` (called- # lazily via `LazyAdminRegistry` on first `admin.site._registry` access), which- # keeps every product/admin import out of `django.setup()`.+ # startup. We invoke autodiscover ourselves from `register_all_admin()` (called by+ # the admin URL conf in `ee/urls.py`, and by `LazyAdminRegistry`), which keeps+ # every product/admin import out of `django.setup()`."django.contrib.admin.apps.SimpleAdminConfig","django.contrib.auth","django.contrib.contenttypes",diff --git a/posthog/test/repo_invariants/test_admin_url_conf.py b/posthog/test/repo_invariants/test_admin_url_conf.pynew file mode 100644index 000000000000..16b4a614a323--- /dev/null+++ b/posthog/test/repo_invariants/test_admin_url_conf.py@@ -0,0 +1,24 @@+from django.contrib import admin+from django.urls import NoReverseMatch, reverse++from posthog.admin import register_all_admin+++def test_admin_app_list_url_covers_every_registered_admin_app():+ # The URL conf has to load first, because `AdminSite.get_urls()` freezes the+ # `admin:app_list` pattern over the registry it finds and never rebuilds it.+ # The second line then registers anything the URL conf left out.+ reverse("admin:index")+ register_all_admin()++ unreachable = []+ for app_label in sorted({model._meta.app_label for model in admin.site._registry}):+ try:+ reverse("admin:app_list", kwargs={"app_label": app_label})+ except NoReverseMatch:+ unreachable.append(app_label)++ assert not unreachable, (+ f"These admin apps registered after the admin URL conf was built: {unreachable}. "+ "The admin URL conf must call register_all_admin() before it reads admin.site.urls."+ )diff --git a/products/data_modeling/backend/test/test_dag_admin.py b/products/data_modeling/backend/test/test_dag_admin.pyindex 85d42fc6ab6a..cae8f243b7bf 100644--- a/products/data_modeling/backend/test/test_dag_admin.py+++ b/products/data_modeling/backend/test/test_dag_admin.py@@ -14,10 +14,10 @@ from products.data_modeling.backend.models.data_modeling_job import DataModelingfrom products.data_modeling.backend.models.node import Node, NodeTypefrom products.data_modeling.backend.test.helpers import saved_query_node-# `posthog/apps.py` installs the lazy admin registry only `if not settings.TEST`, and that wrapper-# is the sole caller of `register_all_admin()` — so under tests nothing ever registers a-# product-local admin. (Autodiscovery itself is fine: this module is exactly what-# `autodiscover_modules("admin")` imports for this app in a real process.)+# `posthog/apps.py` installs the lazy admin registry only `if not settings.TEST`, so under tests+# nothing registers a product-local admin until the admin URL conf loads. Register here, so the+# assertions below do not depend on a URL being resolved first. (Autodiscovery itself is fine:+# this module is what `autodiscover_modules("admin")` imports for this app in a real process.)register_all_admin()