#24414: Add example of Prefetch object usage to docs
-------------------------------+------------------------------------
     Reporter:  coolRR         |                    Owner:  nobody
         Type:  New feature    |                   Status:  new
    Component:  Documentation  |                  Version:  1.7
     Severity:  Normal         |               Resolution:
     Keywords:                 |             Triage Stage:  Accepted
    Has patch:  0              |      Needs documentation:  0
  Needs tests:  0              |  Patch needs improvement:  0
Easy pickings:  0              |                    UI/UX:  0
-------------------------------+------------------------------------

Comment (by sww):

 Here's my stab at an example:


 {{{

 >>>
 
Question.objects.prefetch_related(Prefetch('choice_set')).get().choice_set.all()
 [<Choice: Not much>, <Choice: The sky>, <Choice: Just hacking again>]

 >>> voted_choices = Choice.objects.filter(votes__gt=0)
 >>> voted_choices
 [<Choice: The sky>]

 >>> Question.objects.prefetch_related(Prefetch('choice_set',
 queryset=voted_choices)).get().choice_set.all()
 [<Choice: The sky>]

 # Providing the to_attr value will preserve the choice_set results.
 >>> Question.objects.prefetch_related(Prefetch('choice_set',
 queryset=voted_choices, to_attr='voted_choices')).get().voted_choices
 [<Choice: The sky>]
 >>> Question.objects.prefetch_related(Prefetch('choice_set',
 queryset=voted_choices, to_attr='voted_choices')).get().choice_set.all()
 [<Choice: Not much>, <Choice: The sky>, <Choice: Just hacking again>]

 }}}

 Feedback?

--
Ticket URL: <https://code.djangoproject.com/ticket/24414#comment:3>
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.c91dcaf8101c40d311601dafcb368bc5%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to