#35317: Add the possibility to do prefetches for only a subset of instances
-------------------------------------+-------------------------------------
     Reporter:  Laurent Lyaudet      |                    Owner:  nobody
         Type:  New feature          |                   Status:  new
    Component:  Database layer       |                  Version:  5.0
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Comment (by Simon Charette):

 I'm not convinced we should add a feature for that given
 `prefetch_related_objects` is already
 
[https://docs.djangoproject.com/en/5.0/ref/models/querysets/#django.db.models.prefetch_related_objects
 a documented public API] that allows for this feature to be built on top
 of

 {{{#!python
 orders = (
     Order.objects.filter(...)
     .exclude(...)
     .annotate(...)
     .prefetch_related(
         "items",
         "address",
     )
 )
 predicate = lambda packing_task:
 order.needs_to_consider_packing_problematic_zonings()
 problematic_orders = filter(predicate, orders)
 prefetch_related_objects(
     orders,
     Prefetch(
         "packing_task__zonings",
         queryset=Zonings.objects.filter(...),
         to="problematic_zonings"
     ),
 )
 }}}

 I'd a bit more trickier when dealing with predicates for nested prefetches
 but it's still doable.

 Just like any other non-trivial feature addition like this one I suggest
 trying to gather consensus on a general need for
 [https://docs.djangoproject.com/en/5.0/internals/contributing/bugs-and-
 features/#requesting-features this feature] on the forum and / or
 developer mailing list. From providing support over this channels in the
 past years I don't remember anyone else running into a similar issue that
 couldn't be solved with `prefetch_related_objects` directly.
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35317#comment:4>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018e59469b34-392235b1-3fdc-477a-b685-54d994a1a822-000000%40eu-central-1.amazonses.com.

Reply via email to