Comment (by Simon Charette):
I agree with Mariusz's conclusion. If you're looking for a solution to
catch these problems early through in development on in your test suite
without peppering with it `assertNumQueries` I suggest you look into
[https://github.com/charettes/django-seal third-party solutions] that will
emit warnings when such problems occur.
In your particular case you would have gotten a warning along the lines of
`UnsealedAttributeAccess: Attempt to fetch deferred field "publisher_id"
on sealed <Book instance>`
Which can be elevated to an error during development or tests via
`warnings.filterwarnings('error', category=UnsealedAttributeAccess)`
This origin of this package was the exact same as yours; we needed a tool
to allow the efficient creation of Queryset for a GraphQL querying
endpoint and wanted to catch N+1 query problems due to missing
`select_related`, `prefetch_related` or too eager usage of field deferral.
--
Ticket URL: <https://code.djangoproject.com/ticket/33835#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.