From b1962e1e6d33357fe6813283c9be5b429ef5a829 Mon Sep 17 00:00:00 2001 From: Chris Doehring Date: Mon, 1 Jun 2026 16:37:06 -0700 Subject: [PATCH] fix: autocomplete inline FKs to stop Route admin change page timing out The Django admin Route change page renders RouteProviderInline and RouteDestinationInline, each of which renders a of *every* Integration, + # and Integration.__str__ touches owner.name/type.name (not select_related), + # making the Route change page an N+1 storm that times out in production. + autocomplete_fields = ("integration",) class RouteDestinationInline(admin.TabularInline): model = Route.destinations.through + autocomplete_fields = ("integration",) @admin.register(Route) diff --git a/cdip_admin/integrations/tests/test_admin.py b/cdip_admin/integrations/tests/test_admin.py new file mode 100644 index 000000000..5964efd2d --- /dev/null +++ b/cdip_admin/integrations/tests/test_admin.py @@ -0,0 +1,53 @@ +import pytest +from django.db import connection +from django.test.utils import CaptureQueriesContext +from django.urls import reverse + +from integrations.models import Integration + +pytestmark = pytest.mark.django_db + + +def _render_query_count(admin_client, url): + with CaptureQueriesContext(connection) as ctx: + response = admin_client.get(url) + assert response.status_code == 200, response.status_code + return len(ctx.captured_queries) + + +def test_route_change_page_does_not_scale_with_integration_count( + admin_client, route_1, organization, integration_type_er +): + """The Route change page renders RouteProvider/RouteDestination inlines. + + Without autocomplete_fields, each inline renders a