Re: [Django] #33835: Select_related().only() in the Prefetch() should automatically add primary keys for reverse relations.

2 views
Skip to first unread message

Django

unread,
Jul 11, 2022, 9:26:52 AM7/11/22
to django-...@googlegroups.com
#33835: Select_related().only() in the Prefetch() should automatically add primary
keys for reverse relations.
-------------------------------------+-------------------------------------
Reporter: Ipakeev | Owner: nobody
Type: New feature | Status: closed
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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.

Reply all
Reply to author
Forward
0 new messages