#24873: Error while using Prefech on more than two levels
-------------------------------------+-------------------------------------
     Reporter:  Gagaro               |      Owner:  nobody
         Type:  Bug                  |     Status:  new
    Component:  Database layer       |    Version:  master
  (models, ORM)                      |   Keywords:  Prefetch
     Severity:  Normal               |  prefetch_related
 Triage Stage:  Unreviewed           |  Has patch:  0
Easy pickings:  0                    |      UI/UX:  0
-------------------------------------+-------------------------------------
 Given the following models:


 {{{
 class A(models.Model):
     pass


 class B(models.Model):
     a = models.ForeignKey(A, related_name='b')


 class C(models.Model):
     b = models.ForeignKey(B, related_name='c')


 class D(models.Model):
     c = models.ForeignKey(C, related_name='d')
 }}}

 And the following tests:

 {{{
 class PrefetchTest(TestCase):
     def setUp(self):
         a = A.objects.create()
         b = B.objects.create(a=a)
         c = C.objects.create(b=b)
         D.objects.create(c=c)

     def test_with_prefetch(self):
         c = C.objects.prefetch_related(Prefetch('d'))
         b = B.objects.prefetch_related(Prefetch('c', queryset=c))
         a = A.objects.prefetch_related(Prefetch('b', queryset=b))
         list(a)

     def test_without_prefetch(self):
         c = C.objects.prefetch_related('d')
         b = B.objects.prefetch_related(Prefetch('c', queryset=c))
         a = A.objects.prefetch_related(Prefetch('b', queryset=b))
         list(a)
 }}}

 The first test will fail while the second will succeed. The backtrace with
 Django 1.8 is:


 {{{
 Traceback (most recent call last):
   File
 "/home/gagaro/django/modules/test/django_test/models_test/tests.py", line
 19, in test_with_prefetch
     list(a)
   File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-
 packages/django/db/models/query.py", line 162, in __iter__
     self._fetch_all()
   File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-
 packages/django/db/models/query.py", line 967, in _fetch_all
     self._prefetch_related_objects()
   File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-
 packages/django/db/models/query.py", line 591, in
 _prefetch_related_objects
     prefetch_related_objects(self._result_cache,
 self._prefetch_related_lookups)
   File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-
 packages/django/db/models/query.py", line 1516, in
 prefetch_related_objects
     obj_list, additional_lookups = prefetch_one_level(obj_list,
 prefetcher, lookup, level)
   File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-
 packages/django/db/models/query.py", line 1615, in prefetch_one_level
     prefetcher.get_prefetch_queryset(instances,
 lookup.get_current_queryset(level)))
   File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-
 packages/django/db/models/fields/related.py", line 729, in
 get_prefetch_queryset
     for rel_obj in queryset:
   File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-
 packages/django/db/models/query.py", line 162, in __iter__
     self._fetch_all()
   File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-
 packages/django/db/models/query.py", line 967, in _fetch_all
     self._prefetch_related_objects()
   File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-
 packages/django/db/models/query.py", line 591, in
 _prefetch_related_objects
     prefetch_related_objects(self._result_cache,
 self._prefetch_related_lookups)
   File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-
 packages/django/db/models/query.py", line 1505, in
 prefetch_related_objects
     (through_attr, first_obj.__class__.__name__, lookup.prefetch_through))
 AttributeError: Cannot find 'c' on C object, 'c__d' is an invalid
 parameter to prefetch_related()
 }}}

 I tested with Django 1.7, 1.8, 1.8.2 and master (commit
 24718b7dccd64dfa118fe8136e7b175babec679b).

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

Reply via email to