diff --git a/tests/select_related_regress/models.py b/tests/select_related_regress/models.py index 620b8eb5ea..f98b8269a8 100644 --- a/tests/select_related_regress/models.py +++ b/tests/select_related_regress/models.py @@ -108,3 +108,21 @@ class Hen(Fowl): class Chick(Fowl): mother = models.ForeignKey(Hen) + +class Base(models.Model): + name = models.CharField(max_length=10) + lots_of_text = models.TextField() + + class Meta: + abstract = True + +class A(Base): + a_field = models.CharField(max_length=10) + +class B(Base): + b_field = models.CharField(max_length=10) + +class C(Base): + c_a = models.ForeignKey(A) + c_b = models.ForeignKey(B) + is_published = models.BooleanField() diff --git a/tests/select_related_regress/tests.py b/tests/select_related_regress/tests.py index da47cb771f..08ada4515e 100644 --- a/tests/select_related_regress/tests.py +++ b/tests/select_related_regress/tests.py @@ -5,7 +5,7 @@ from django.utils import six from .models import (Building, Child, Device, Port, Item, Country, Connection, ClientStatus, State, Client, SpecialClient, TUser, Person, Student, - Organizer, Class, Enrollment, Hen, Chick) + Organizer, Class, Enrollment, Hen, Chick, Base, A, B, C) class SelectRelatedRegressTests(TestCase): @@ -173,3 +173,19 @@ class SelectRelatedRegressTests(TestCase): self.assertEqual(Chick.objects.all()[0].mother.name, 'Hen') self.assertEqual(Chick.objects.select_related()[0].mother.name, 'Hen') + + def test_ticket_10733(self): + a = A.objects.create(name='a', lots_of_text='lots_of_text_a', a_field='a_field') + b = B.objects.create(name='b', lots_of_text='lots_of_text_b', b_field='b_field') + c = C.objects.create(name='c', lots_of_text='lots_of_text_c', is_published=True, + c_a=a, c_b=b) + results = C.objects.all().only('name', 'lots_of_text', 'c_a', 'c_b', 'c_b__lots_of_text', + 'c_a__name', 'c_b__name').select_related() + self.assertQuerysetEqual(results, [c], lambda x: x) + with self.assertNumQueries(0): + qs_c = results[0] + self.assertEqual(qs_c.name, 'c') + self.assertEqual(qs_c.lots_of_text, 'lots_of_text_c') + self.assertEqual(qs_c.c_b.lots_of_text, 'lots_of_text_b') + self.assertEqual(qs_c.c_a.name, 'a') + self.assertEqual(qs_c.c_b.name, 'b')