Changes between Initial Version and Version 2 of Ticket #24873


Ignore:
Timestamp:
May 29, 2015, 9:54:46 AM (9 years ago)
Author:
Gagaro
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #24873

    • Property Triage Stage UnreviewedAccepted
  • Ticket #24873 – Description

    initial v2  
    33
    44{{{
     5# Foreign Keys
     6
    57class A(models.Model):
    68    pass
    7 
    89
    910class B(models.Model):
    1011    a = models.ForeignKey(A, related_name='b')
    1112
    12 
    1313class C(models.Model):
    1414    b = models.ForeignKey(B, related_name='c')
    1515
    16 
    1716class D(models.Model):
    1817    c = models.ForeignKey(C, related_name='d')
     18
     19
     20#ManyToMany Fields
     21
     22class E(models.Model):
     23    pass
     24
     25class F(models.Model):
     26    e = models.ManyToManyField(E, related_name='f')
     27
     28class G(models.Model):
     29    f = models.ManyToManyField(F, related_name='g')
     30
     31class H(models.Model):
     32    g = models.ManyToManyField(G, related_name='h')
    1933}}}
    2034
     
    2236
    2337{{{
    24 class PrefetchTest(TestCase):
     38class PrefetchForeignKeyTest(TestCase):
    2539    def setUp(self):
    2640        a = A.objects.create()
     
    3448        a = A.objects.prefetch_related(Prefetch('b', queryset=b))
    3549        list(a)
     50        a.first().b.first().c.first().d.first()
    3651
    3752    def test_without_prefetch(self):
     
    4055        a = A.objects.prefetch_related(Prefetch('b', queryset=b))
    4156        list(a)
     57        a.first().b.first().c.first().d.first()
     58
     59
     60class PrefetchManyTest(TestCase):
     61    def setUp(self):
     62        e = E.objects.create()
     63        f = F.objects.create()
     64        g = G.objects.create()
     65        h = H.objects.create()
     66        f.e.add(e)
     67        g.f.add(f)
     68        h.g.add(g)
     69
     70    def test_with_prefetch(self):
     71        g = G.objects.prefetch_related(Prefetch('h'))
     72        f = F.objects.prefetch_related(Prefetch('g', queryset=g))
     73        e = E.objects.prefetch_related(Prefetch('f', queryset=f))
     74        list(e)
     75        e.first().f.first().g.first().h.first()
     76
     77    def test_without_prefetch(self):
     78        g = G.objects.prefetch_related('h')
     79        f = F.objects.prefetch_related(Prefetch('g', queryset=g))
     80        e = E.objects.prefetch_related(Prefetch('f', queryset=f))
     81        list(e)
     82        e.first().f.first().g.first().h.first()
    4283}}}
    4384
    44 The first test will fail while the second will succeed. The backtrace with Django 1.8 is:
    45 
     85The tests will fail. The backtrace with master is:
    4686
    4787{{{
     88======================================================================
     89ERROR: test_with_prefetch (models_test.tests.PrefetchManyTest)
     90----------------------------------------------------------------------
    4891Traceback (most recent call last):
    49   File "/home/gagaro/django/modules/test/django_test/models_test/tests.py", line 19, in test_with_prefetch
     92  File "/home/gagaro/django/modules/test/django_test/models_test/tests.py", line 49, in test_with_prefetch
     93    e.first().f.first().g.first().h.first()
     94  File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-packages/django/db/models/query.py", line 543, in first
     95    objects = list((self if self.ordered else self.order_by('pk'))[:1])
     96  File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-packages/django/db/models/query.py", line 258, in __iter__
     97    self._fetch_all()
     98  File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-packages/django/db/models/query.py", line 1065, in _fetch_all
     99    self._prefetch_related_objects()
     100  File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-packages/django/db/models/query.py", line 649, in _prefetch_related_objects
     101    prefetch_related_objects(self._result_cache, self._prefetch_related_lookups)
     102  File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-packages/django/db/models/query.py", line 1426, in prefetch_related_objects
     103    (through_attr, first_obj.__class__.__name__, lookup.prefetch_through))
     104AttributeError: Cannot find 'f' on F object, 'f__f__g' is an invalid parameter to prefetch_related()
     105
     106======================================================================
     107ERROR: test_without_prefetch (models_test.tests.PrefetchManyTest)
     108----------------------------------------------------------------------
     109Traceback (most recent call last):
     110  File "/home/gagaro/django/modules/test/django_test/models_test/tests.py", line 56, in test_without_prefetch
     111    e.first().f.first().g.first().h.first()
     112  File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-packages/django/db/models/query.py", line 543, in first
     113    objects = list((self if self.ordered else self.order_by('pk'))[:1])
     114  File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-packages/django/db/models/query.py", line 258, in __iter__
     115    self._fetch_all()
     116  File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-packages/django/db/models/query.py", line 1065, in _fetch_all
     117    self._prefetch_related_objects()
     118  File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-packages/django/db/models/query.py", line 649, in _prefetch_related_objects
     119    prefetch_related_objects(self._result_cache, self._prefetch_related_lookups)
     120  File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-packages/django/db/models/query.py", line 1426, in prefetch_related_objects
     121    (through_attr, first_obj.__class__.__name__, lookup.prefetch_through))
     122AttributeError: Cannot find 'f' on F object, 'f__f__g' is an invalid parameter to prefetch_related()
     123
     124======================================================================
     125ERROR: test_with_prefetch (models_test.tests.PrefetchForeignKeyTest)
     126----------------------------------------------------------------------
     127Traceback (most recent call last):
     128  File "/home/gagaro/django/modules/test/django_test/models_test/tests.py", line 23, in test_with_prefetch
    50129    list(a)
    51   File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-packages/django/db/models/query.py", line 162, in __iter__
     130  File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-packages/django/db/models/query.py", line 258, in __iter__
    52131    self._fetch_all()
    53   File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-packages/django/db/models/query.py", line 967, in _fetch_all
     132  File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-packages/django/db/models/query.py", line 1065, in _fetch_all
    54133    self._prefetch_related_objects()
    55   File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-packages/django/db/models/query.py", line 591, in _prefetch_related_objects
     134  File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-packages/django/db/models/query.py", line 649, in _prefetch_related_objects
    56135    prefetch_related_objects(self._result_cache, self._prefetch_related_lookups)
    57   File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-packages/django/db/models/query.py", line 1516, in prefetch_related_objects
     136  File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-packages/django/db/models/query.py", line 1437, in prefetch_related_objects
    58137    obj_list, additional_lookups = prefetch_one_level(obj_list, prefetcher, lookup, level)
    59   File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-packages/django/db/models/query.py", line 1615, in prefetch_one_level
     138  File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-packages/django/db/models/query.py", line 1536, in prefetch_one_level
    60139    prefetcher.get_prefetch_queryset(instances, lookup.get_current_queryset(level)))
    61   File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 729, in get_prefetch_queryset
     140  File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 753, in get_prefetch_queryset
    62141    for rel_obj in queryset:
    63   File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-packages/django/db/models/query.py", line 162, in __iter__
     142  File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-packages/django/db/models/query.py", line 258, in __iter__
    64143    self._fetch_all()
    65   File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-packages/django/db/models/query.py", line 967, in _fetch_all
     144  File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-packages/django/db/models/query.py", line 1065, in _fetch_all
    66145    self._prefetch_related_objects()
    67   File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-packages/django/db/models/query.py", line 591, in _prefetch_related_objects
     146  File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-packages/django/db/models/query.py", line 649, in _prefetch_related_objects
    68147    prefetch_related_objects(self._result_cache, self._prefetch_related_lookups)
    69   File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-packages/django/db/models/query.py", line 1505, in prefetch_related_objects
     148  File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-packages/django/db/models/query.py", line 1426, in prefetch_related_objects
    70149    (through_attr, first_obj.__class__.__name__, lookup.prefetch_through))
    71150AttributeError: Cannot find 'c' on C object, 'c__d' is an invalid parameter to prefetch_related()
     151
     152======================================================================
     153ERROR: test_without_prefetch (models_test.tests.PrefetchForeignKeyTest)
     154----------------------------------------------------------------------
     155Traceback (most recent call last):
     156  File "/home/gagaro/django/modules/test/django_test/models_test/tests.py", line 31, in test_without_prefetch
     157    a.first().b.first().c.first().d.first()
     158  File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-packages/django/db/models/query.py", line 543, in first
     159    objects = list((self if self.ordered else self.order_by('pk'))[:1])
     160  File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-packages/django/db/models/query.py", line 258, in __iter__
     161    self._fetch_all()
     162  File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-packages/django/db/models/query.py", line 1065, in _fetch_all
     163    self._prefetch_related_objects()
     164  File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-packages/django/db/models/query.py", line 649, in _prefetch_related_objects
     165    prefetch_related_objects(self._result_cache, self._prefetch_related_lookups)
     166  File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-packages/django/db/models/query.py", line 1437, in prefetch_related_objects
     167    obj_list, additional_lookups = prefetch_one_level(obj_list, prefetcher, lookup, level)
     168  File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-packages/django/db/models/query.py", line 1536, in prefetch_one_level
     169    prefetcher.get_prefetch_queryset(instances, lookup.get_current_queryset(level)))
     170  File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 753, in get_prefetch_queryset
     171    for rel_obj in queryset:
     172  File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-packages/django/db/models/query.py", line 258, in __iter__
     173    self._fetch_all()
     174  File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-packages/django/db/models/query.py", line 1065, in _fetch_all
     175    self._prefetch_related_objects()
     176  File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-packages/django/db/models/query.py", line 649, in _prefetch_related_objects
     177    prefetch_related_objects(self._result_cache, self._prefetch_related_lookups)
     178  File "/home/gagaro/.virtualenvs/django-test/local/lib/python2.7/site-packages/django/db/models/query.py", line 1426, in prefetch_related_objects
     179    (through_attr, first_obj.__class__.__name__, lookup.prefetch_through))
     180AttributeError: Cannot find 'b' on B object, 'b__c' is an invalid parameter to prefetch_related()
    72181}}}
    73182
Back to Top