unused_eager_loading
Detect unused select_related and prefetch_related calls in Django serializers to eliminate unnecessary database queries and optimize performance.
Instructions
select_related and prefetch_related the serializer never reads.
Every tool in this space looks the other way: a relation the serializer
touches that the queryset did not prefetch, which is the N+1. This is the
opposite, and it costs on every request. An unused select_related is a
JOIN on every row; an unused prefetch_related is a whole extra query plus
the objects it returns.
It is invisible because it looks like an optimisation, and it usually was
one - the field it was added for was removed and nobody takes the line out,
because removing one feels riskier than leaving it in.
nplusone finds this at runtime by watching which loaded objects go
untouched, so it covers the paths the tests exercise. Both halves are in
the source: the queryset says what it loads, the serializer what it reads.
Args:
include_low_confidence: also report views whose serializer has a
SerializerMethodField or which override list/retrieve/
to_representation, where the relation may be read out of sight.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| include_low_confidence | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||