Opened 21 months ago

Closed 21 months ago

Last modified 17 months ago

#34413 closed New feature (duplicate)

Variant of Prefetch but for the earliest/latest related object — at Version 4

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 (last modified by Willem Van Onsem)

What would happen if we span over multiple relations, like:

User.objects.prefetch_related(
    Prefetch("groups__permissions", queryset=Permission.objects.order_by('codename')[:1], to_attr="first_permission")
)

Change History (5)

comment:1 by Willem Van Onsem, 21 months ago

Needs tests: set

comment:2 by Willem Van Onsem, 21 months ago

Description: modified (diff)

comment:3 by Simon Charette, 21 months ago

Resolution: duplicate
Status: assignedclosed

Pretty sure this is a duplicate of #26780 fixed in the soon to be released 4.2.

Since 242499f2dc2bf24a9a5c855690a2e13d3303581a Prefetch(queryset) supports slicing through the use of filtering over partitioned rank (RANK() OVER (PARTITION BY <join_field> ORDER BY <order_by>) so earliest and latest related objects (or any slice of related objects for that matter) can be prefetched.

User.objects.prefetch_related(
    Prefetch("groups", queryset=Group.objects.order_by('name')[:1], to_attr="first_group")
)

comment:4 by Willem Van Onsem, 21 months ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top