Prefetch the artifact-family admin changelists (#1346, Phase 3)#1350
Merged
Conversation
The Publication/Talk/Poster/Video/Grant/Award list pages each rendered M2M/FK callable columns (author & speaker lists, projects, sponsor, recipients) with no prefetch, firing 1-3 queries per row. - get_queryset() now prefetch_related()s the relations each changelist renders: Publication (authors, projects), Talk (authors), Poster (authors), Video (projects), Grant (authors), Award (recipients, projects). Grant's sponsor FK uses list_select_related. - Artifact.get_first_author_last_name rewritten to read list(self.authors.all()) instead of .exists()/.first() (which bypass a prefetch cache); SortedManyToManyField preserves order so [0] is still the first author. PublicationAdmin.display_authors likewise reads the cached list instead of a [:5] queryset slice + .count(). Note: Django has no `list_prefetch_related` ModelAdmin option (only list_select_related), so prefetching must go through get_queryset(). Tests (test_admin_perf_artifacts.py): correctness of the two rewritten methods, plus a steady-state guard that each of the five changelists stays flat as rows grow 3 -> 6. Full suite green (401). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Phase 3 of #1346 (admin changelist audit). The artifact list pages — Publication, Talk, Poster, Video, Grant, Award — each render M2M/FK callable columns (author & speaker lists, projects, sponsor, recipients) with no prefetch, so they fired 1–3 queries per row. Now each is constant-query.
Changes
get_queryset()prefetch on each admin for exactly what its changelist renders:authors,projectsauthors(speakers)authorsprojectsauthors(sponsor FK handled bylist_select_related)recipients,projectsArtifact.get_first_author_last_namerewritten to readlist(self.authors.all())instead of.exists()+.first()(both bypass a prefetch cache).authorsis aSortedManyToManyField, so.all()preserves the editor-defined order —[0]is still the first author. (Used by Poster & Grant columns; kept behavior-identical.)PublicationAdmin.display_authorsnow reads the cached list instead of a[:5]queryset slice +.count()(both re-queried per row).Heads-up / gotcha
There is no
list_prefetch_relatedModelAdmin option in Django (onlylist_select_related) — I initially used it, the tests caught that it's a silent no-op, and I moved the prefetch intoget_queryset(). Worth knowing for future changelist work.Tests (
test_admin_perf_artifacts.py)display_authorsfive-names-plus-ellipsis and the no-authors case).get_querysetfix — that's how thelist_prefetch_relatedno-op was caught).Full suite green locally (401 tests). Branches cleanly off current master (Phases 1 & 2 already merged).
🤖 Generated with Claude Code