Re:Understanding N + 1 queries problem
by VWWHFSfQ on Monday January 02, 2023 @05:14PM
(#34224414)
Django will do something similar (possibly a little more elegantly) if one is familiar with how to use the Prefetch APIs [1]:
This will generate the following two queries:
[1] https://docs.djangoproject.com/en/4.1/ref/models/querysets/#...
[ Parent
| on HN ]
Personally, I find the Rails version a bit more elegant because it is declarative, reusable and composable, while Django's approach is a more utilitarian "just write the dang query when you want it." But both are a great illustration of how a well-designed ORM can give you the tools you need to get good performance.
You can do the same kind of thing in Django. The approach is a little different, but it's the same basic idea.
https://docs.djangoproject.com/en/4.1/topics/db/managers/#cu...