Opened 21 months ago
Last modified 17 months ago
#34413 closed New feature
Variant of Prefetch but for the earliest/latest related object — at Initial Version
Reported by: | Willem Van Onsem | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | yes | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
A frequently asked feature that seems to be missing is fetch the earlest/latest related object for each item. Indeed, we can for example work with a subquery to fetch *the primary key* of the earliest/latest related object, but not that object itself.
It turns out however that automating this is not that complicated. What we need to do is (automatically) construct a reverse filter, so if we want the latest Comment
of each Post
, we make a queryset that makes a Comment.objects.filter(post_id=OuterRef('pk'))
, then we convert that into a subquery that will, for each Post
fetch the primary key of the latest comment, and slightly alter the logic that is already used to for prefetching to then prefetch all the Comment
s for these primary keys, and add attributes to the corresponding Post
s.
I made a small proof-of-concept that should normally work for (most) cases: it allows one to use an arbitrary queryset and specify an ordering (or use the ordering already in the queryset or by the model). It can also span over multiple layers. It requires to *always* specify a to_attr
, since, especially when spanning over multiple relations, the default to_attr
would require double underscores.
The most ugly part is that we work with an annotation that is then accessible by the user, so it is not somehow hidden. Technically we could remove the attribute, or do something extra in the ORM to prevent exposing this attribute.
I did not yet check what querysets will be problematic. For example a sliced queryset would (very likely) not work, and likely most/all restrictions already in place for a the queryset of a Prefetch
object are still applicable.
Change History (1)
by , 21 months ago
Attachment: | prefetching_the_earliest_latest_related_object_.patch added |
---|