Read the claim as two checkable statements#
An automated reviewer that reports an N+1 query is asserting two things. Something rendered per row, a template, a serializer field or a property, touches a related object. And the queryset that feeds that loop does not load the relation in advance. Django's select_related() exists for the second part: it "results in a single more complex query but means later use of foreign-key relationships won't require database queries." prefetch_related() does the same for many-valued relations with one extra query per relation.
Either statement can be false. The loop may never touch the relation, or the queryset feeding it may already load what it needs. Each one takes a minute to check. A September 2026 pull request in Gyrinx, a Django application, contains both outcomes.
A claim the author measured and declined#
The pull request drew each user's badge beside their name on campaign screens. In its review, Copilot said a shared campaign row template "is also rendered by the signed-in dashboard", whose queryset "does not select the owner profile or prefetch owner__badge_grants", "so a page with several campaigns regresses to N+1." Copilot review
The author answered: "declined, no change. The signed-in dashboard does not render core/campaign/includes/campaign_row.html". The dashboard draws its own row, "which draws the campaign's name, its status badge and a chevron, and never names the owner". They also measured it: "the dashboard costs 20 queries with one campaign listed and 20 with five (warm session, CaptureQueriesContext), so nothing grows with the number of rows." Author's reply
The reply holds against the merged code. At the merge commit, the dashboard template core/index.html includes core/includes/home/campaign_row.html, and that file renders the campaign name, a status badge and a link, with no owner. The dashboard view also slices its unpinned campaigns with [:5], so even a real per-row lookup there would cost at most five queries, plus one per pinned campaign. Merged dashboard view The reviewer had read the queryset correctly and assumed the wrong template.
A claim a query-count test confirmed#
A day later, Copilot flagged the view that adds a gang to a campaign. The view skipped loading the owner's badge data for every POST, but "can render the template after an invalid form or a refused POST", so those redraws "perform badge lookups during error handling (and can regress the page's query count)." Copilot review
The author confirmed it: the redraws "lazy-loaded both — two queries more than the GET." They fixed it by always joining the owner's profile, and pinned the fix with a test that "posts a gang key the list did not offer and asserts the redraw's query count equals the GET's, and stays flat from one player to four." It "fails against the reviewed guard (23 vs 21) and passes with the join." Author's reply The pull request merged on 13 September 2026 with the test:
address = reverse("n26-campaign-add-gang", args=[table.pk])
player(table, "vex")
on_get = self._queries(client, address)
with_one = redraw(address)
assert with_one == on_get
player(table, "kesh")
player(table, "ash")
player(table, "nyx")
with_more = redraw(address)
assert with_more == with_one
The test compares counts rather than pinning one. It asserts that four players cost what one costs, so an unrelated query added to the page later does not break it, while a per-row lookup does. Django's assertNumQueries pins an exact number instead, which is simpler to write and breaks more often.
Decide from the counts#
| What you find | Decision |
|---|---|
| The per-row code never reads the relation the comment names | Dismiss. Cite the template or serializer line |
| The relation is read per row, but code caps the rows at a small number, such as a slice or a fixed set of choices | Dismiss or defer. State the cap in your reply |
| The query count grows between one row and several | Change. Add select_related() or prefetch_related() and keep the two-size comparison as a test |
| You cannot run the page, but the query log shows the same statement repeated with different ids | Change, then add the test |
| The count is flat but high | This is not an N+1. Answer the comment and look at the count separately |
To get the counts, wrap the request in django.test.utils.CaptureQueriesContext, as both replies above did, or read the django.db.backends logger, which logs every application-level SQL statement at DEBUG level, but only when settings.DEBUG is True. Run it at two row counts with the same session state, because the first request on a cold session often pays for lookups that later requests skip.
Size the rows before you weigh the fix#
Two rows and five rows tell you whether the count grows. They do not tell you whether it matters. Use the largest row count the page can show in ordinary use. A list capped at five costs at most five extra queries. An organization's member list may hold hundreds. When the reviewer could not see the cap or the template, as in the dashboard case, the comment is a reasonable question, and the dismissed finding guide shows how to record the answer so it is not counted as noise. If the bot raises the same claim on the next push, the measured reply is what you point to.