#27554: Queryset evaluation fails with mix of nested and flattened prefetches
(AttributeError on RelatedManager)
-------------------------------------+-------------------------------------
     Reporter:  Anthony Leontiev     |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  1.10
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  prefetch             |             Triage Stage:  Accepted
  AttributeError prefetch_related    |
  nested RelatedManager              |
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Vimarsh Chaturvedi):

 Hey guys,

 My first time posting here.

 I tried reproducing the bug, and was able to.
 I added the following cases as per Simon Charette's suggestion and Django
 didn't throw any exception.

 views.py
 {{{
 #Code in orinigal test project by Anthony Leontiev
 def setup():
     a = A.objects.create()
     b = B.objects.create()

     b.a = a
     b.save()

     d = D.objects.create()
     C.objects.create(b=b, d=d)


 #Succeeded: As reported above.
 def nested_prefetch(request, *args, **kwargs):
     setup()
     queryset = A.objects.all().prefetch_related(
         Prefetch(
             'b',
             queryset=B.objects.all().prefetch_related(
                 Prefetch(
                     'c',
                     queryset=C.objects.all().prefetch_related(
                         'd'
                     )
                 )
             )
         )
     )
     content = queryset[0].b.c.all()[0].d.pk
     return HttpResponse(content)

 #Failed: As reported above.
 def flat_prefetch(request, *args, **kwargs):
     setup()
     queryset = A.objects.all().prefetch_related(
         Prefetch(
             'b',
             queryset=B.objects.all().prefetch_related(
                 Prefetch('c__d')
             )
         )
     )
     content = queryset[0].b.c.all()[0].d.pk
     return HttpResponse(content)

 #The following three cases were added by me
 #Failed: As expected.
 def flat_prefetch_one(request, *args, **kwargs):
     setup()
     queryset = A.objects.all().prefetch_related(
         Prefetch(
             'b',
             queryset=B.objects.all().prefetch_related('c__d')
         )
     )
     content = queryset[0].b.c.all()[0].d.pk
     return HttpResponse(content)

 #Succeeded: I  was expecting this and the following case to fail too, but
 they didn't.
 def flat_prefetch_two(request, *args, **kwargs):
     setup()
     queryset = A.objects.all().prefetch_related(
         Prefetch('b__c__d')
     )
     content = queryset[0].b.c.all()[0].d.pk
     return HttpResponse(content)

 #succeeded
 def flat_prefetch_three(request, *args, **kwargs):
     setup()
     queryset = A.objects.all().prefetch_related('b__c__d')
     content = queryset[0].b.c.all()[0].d.pk
     return HttpResponse(content)
 }}}

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

Reply via email to