Skip to content

fix: Route admin change page timeout (N+1 in inline integration dropdowns)#425

Closed
chrisdoehring wants to merge 1 commit into
mainfrom
fix-route-admin-timeout
Closed

fix: Route admin change page timeout (N+1 in inline integration dropdowns)#425
chrisdoehring wants to merge 1 commit into
mainfrom
fix-route-admin-timeout

Conversation

@chrisdoehring

Copy link
Copy Markdown
Contributor

Problem

The Route admin change page (e.g. /admin/integrations/route/<id>/change/) fails to load and times out in production.

Root cause

RouteAdmin mounts two inlines on the M2M through models (RouteProviderInline, RouteDestinationInline). Each inline row renders the through model's integration ForeignKey as a plain <select> listing every Integration. Because Integration.__str__ dereferences related objects:

def __str__(self):
    return f"{self.owner.name} - {self.name} - {self.type.name}"

…and the admin's ModelChoiceField queryset has no select_related, rendering each <option> fires 2 extra queries (owner, type). That's 1 + 2N queries per inline row, and there are 8+ rows by default (existing providers + destinations + 3 extra each). With thousands of integrations in production this becomes hundreds of thousands of queries → timeout.

This is distinct from the recent fix-admin-performance work, which addressed COUNT(*) on changelist views; this is the change form.

Measured evidence

Loading a test route's change page:

  • ~12 integrations → 247 queries
  • after adding 25 more → 746 queries

i.e. ~20 queries added per integration — linear scaling with the Integration table.

Fix

Switch the heavy FKs to autocomplete_fields, which renders an AJAX search box instead of an eager dropdown (zero options rendered up front, no N+1):

  • integration on both inlines (IntegrationAdmin already has search_fields)
  • owner + configuration on RouteAdmin (OrganizationAdmin already has search_fields)
  • Added search_fields to RouteConfigAdmin so configuration qualifies for autocomplete

Tests

New integrations/tests/test_admin.py:

  • Scaling regression test — query count to render the change page no longer grows with the Integration table size (fails before, passes after).
  • Widget test — the inline integration field renders as an admin-autocomplete widget.

manage.py check passes (validates the autocomplete_fields / search_fields wiring).

🤖 Generated with Claude Code

The Route admin change page rendered two inlines (RouteProvider,
RouteDestination), each row showing the `integration` FK as an eager
<select> of every Integration. Because Integration.__str__ dereferences
`owner` and `type`, each rendered option triggered an N+1 query — per
option, per inline row. With thousands of integrations the page issued
hundreds of thousands of queries and timed out.

Measured on a test route: 247 queries with ~12 integrations, 746 after
adding 25 more — linear scaling with the Integration table.

Switch the heavy FKs to autocomplete_fields (AJAX search, zero eager
options): `integration` on both inlines and `owner`/`configuration` on
RouteAdmin. Add search_fields to RouteConfigAdmin so its FK qualifies.

Adds integrations/tests/test_admin.py: a scaling regression test (query
count no longer grows with the Integration table) and a widget test.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@chrisdoehring

Copy link
Copy Markdown
Contributor Author

Closing as a duplicate of #424, which was merged ahead of this and fixes the same Route admin change-page timeout (autocomplete_fields on the inline integration FKs). The bug is resolved on main.

@chrisdoehring chrisdoehring deleted the fix-route-admin-timeout branch June 2, 2026 17:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant