Fixed #26486 -- Fixed a select_related() + defer() MTI edge case.

This commit is contained in:
Tim Graham 2016-04-10 15:08:41 -04:00
parent c339a5a6f7
commit 0456a8b9e6
3 changed files with 4 additions and 11 deletions

View File

@ -256,7 +256,6 @@ def select_related_descend(field, restricted, requested, load_fields, reverse=Fa
" and traversed using select_related"
" at the same time." %
(field.model._meta.object_name, field.name))
return False
return True

View File

@ -128,9 +128,8 @@ class DeferTests(AssertionMixin, TestCase):
Primary.objects.only("name").select_related("related")[0]
def test_defer_foreign_keys_are_deferred_and_not_traversed(self):
# With a depth-based select_related, all deferred ForeignKeys are
# deferred instead of traversed.
with self.assertNumQueries(3):
# select_related() overrides defer().
with self.assertNumQueries(1):
obj = Primary.objects.defer("related").select_related()[0]
self.assert_delayed(obj, 1)
self.assertEqual(obj.related.id, self.s1.pk)

View File

@ -1,7 +1,5 @@
from __future__ import unicode_literals
import unittest
from django.core.exceptions import FieldError
from django.test import SimpleTestCase, TestCase
@ -175,7 +173,6 @@ class ReverseSelectRelatedTestCase(TestCase):
self.assertEqual(p.child1.child4.value, p.child1.value)
self.assertEqual(p.child1.child4.value4, 4)
@unittest.expectedFailure
def test_inheritance_deferred(self):
c = Child4.objects.create(name1='n1', name2='n2', value=1, value4=4)
with self.assertNumQueries(1):
@ -192,10 +189,9 @@ class ReverseSelectRelatedTestCase(TestCase):
with self.assertNumQueries(1):
self.assertEqual(p.child1.name2, 'n2')
@unittest.expectedFailure
def test_inheritance_deferred2(self):
c = Child4.objects.create(name1='n1', name2='n2', value=1, value4=4)
qs = Parent2.objects.select_related('child1', 'child4').only(
qs = Parent2.objects.select_related('child1', 'child1__child4').only(
'id2', 'child1__value', 'child1__child4__value4')
with self.assertNumQueries(1):
p = qs.get(name2="n2")
@ -207,9 +203,8 @@ class ReverseSelectRelatedTestCase(TestCase):
with self.assertNumQueries(1):
self.assertEqual(p.child1.name2, 'n2')
p = qs.get(name2="n2")
with self.assertNumQueries(1):
with self.assertNumQueries(0):
self.assertEqual(p.child1.name1, 'n1')
with self.assertNumQueries(1):
self.assertEqual(p.child1.child4.name1, 'n1')