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

 I have use cases with serializers returning a lot of distinct
 informations.
 For example, my serialiser can be called with many=True and a query of
 2000 Order instances.
 And for a small part of these 2000 instances, I need very expensive
 prefetches.
 Currently, I deal with that by overloading the _fetch_all() method and
 make by hand the hyper-specifics and expensive prefetches,
 after everything else.
 What I propose is to add a filter_callback kwarg to Prefetch object.
 filter_callback would be either None (default) or a function taking only
 one instance of the previous level of prefetch
 and return a boolean to know if the instance needs that prefetch.
 A simple example is when you will filter on some status field of Order,
 but in that case, you can just modify the query somehow.
 And unfortunately, the order id would still be injected in the prefetch
 query.
 But for more realistic examples, you do not want to recode the business
 logic
 in  the SQL query to know what needs the prefetch or not.

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

 Best regards,
      Laurent Lyaudet
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35317>
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/0107018e57ed3382-a7beae0d-23cf-4bfc-a82e-c6ecff266bdf-000000%40eu-central-1.amazonses.com.

Reply via email to