From fdd5bb278a03a624cbb43108dad314d5818724a8 Mon Sep 17 00:00:00 2001 From: Jacob Coffee Date: Tue, 11 Aug 2026 13:20:29 -0500 Subject: [PATCH] Fix N+1 queries on nominations admin changelists `NomineeAdmin.list_display` starts with `__str__`, and `Nominee.__str__` resolves to `self.user.first_name`, so the changelist issued one `SELECT users_user...` per row, plus one per row for the `election` column. `NominationAdmin` hit the same thing through its `nominee` column, which renders `Nominee.__str__`. Select the displayed relations in `get_queryset()` on both admins. Refs PYDOTORG-PROD-1N8 Co-Authored-By: Claude Opus 5 (1M context) --- apps/nominations/admin.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/apps/nominations/admin.py b/apps/nominations/admin.py index 6171f4888..b2aa7f603 100644 --- a/apps/nominations/admin.py +++ b/apps/nominations/admin.py @@ -32,6 +32,10 @@ class NomineeAdmin(admin.ModelAdmin): list_filter = ("election", "accepted", "approved") readonly_fields = ("slug",) + def get_queryset(self, request): + """Select the relations rendered in ``list_display`` to avoid per-row queries.""" + return super().get_queryset(request).select_related("user", "election") + def get_ordering(self, request): """Return ordering by election and last name.""" return ["election", Lower("user__last_name")] @@ -61,6 +65,10 @@ class NominationAdmin(admin.ModelAdmin): "eligibility_confirmed", ) + def get_queryset(self, request): + """Select the relations rendered in ``list_display`` to avoid per-row queries.""" + return super().get_queryset(request).select_related("election", "nominee__user") + def get_ordering(self, request): """Return ordering by election and nominee last name.""" return ["election", Lower("nominee__user__last_name")]