#26780: Prefetch objects don't work with slices
-------------------------------------+-------------------------------------
     Reporter:  ludwik               |                    Owner:  nobody
         Type:  New feature          |                   Status:  new
    Component:  Database layer       |                  Version:  master
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  prefetch, slicing    |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by asdator):

 The only way to limit this Query (and to not load all the records into
 memory) would be to is to do a `top-n-per-group` query which I don't think
 the `ORM` is capable of.

 What I can suggest is: Instead of limiting the `QuerySet`, find a way to
 filter it.

 For example, get only the `Posts` in the last day, week or a month (based
 on the post-frequency).

 {{{
 Category.objects.prefetch_related(Prefetch(
     'post_set',
     queryset=Post.objects.filter(
         date_published__gte=datetime.date.today()-timedelta(days=7)),
     to_attr='example_posts',
 ))
 }}}

 This way you won't load all the Posts into Memory.

--
Ticket URL: <https://code.djangoproject.com/ticket/26780#comment:6>
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 post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/064.d5c39a9ac2125222e85253392a83cf69%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to