defeated_prefetches
Detect Django prefetch_related queries that re-query the database due to filters or other operations, exposing hidden N+1 costs.
Instructions
Relations that were prefetched and then re-queried anyway.
A prefetched related manager answers `.count()`, `.exists()`, `.all()` and
a slice from its cache. Anything else goes back to the database, once per
parent object, with the prefetch query already paid for on top:
orders = Order.objects.prefetch_related("lines")
for order in orders:
for line in order.lines.filter(active=True): # one query per order
That costs more than never prefetching at all, and it reads like an
optimisation, which is why it survives review.
Reported only where the prefetch and the accessor are provably the same
object - bound in the same scope, or the loop variable iterating it.
`nplusone` finds the neighbouring problem, an eager load nothing touches,
at runtime; `unused_eager_loading` answers that one statically for DRF.
Args:
search_path: directory to scan. Defaults to the configured project.
include_tests: also report inside test files.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| search_path | No | ||
| include_tests | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||