Fixed #10733 -- select_related().only() failure

The bug was reported pre 1.1, and somewhere along the way it has been
fixed. So, this is tests only addition.
This commit is contained in:
Anssi Kääriäinen 2013-10-01 19:27:20 +03:00
parent 32e40bbe71
commit 8d4b1629d4
2 changed files with 35 additions and 1 deletions

View File

@ -108,3 +108,21 @@ class Hen(Fowl):
class Chick(Fowl): class Chick(Fowl):
mother = models.ForeignKey(Hen) 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()

View File

@ -5,7 +5,7 @@ from django.utils import six
from .models import (Building, Child, Device, Port, Item, Country, Connection, from .models import (Building, Child, Device, Port, Item, Country, Connection,
ClientStatus, State, Client, SpecialClient, TUser, Person, Student, ClientStatus, State, Client, SpecialClient, TUser, Person, Student,
Organizer, Class, Enrollment, Hen, Chick) Organizer, Class, Enrollment, Hen, Chick, Base, A, B, C)
class SelectRelatedRegressTests(TestCase): class SelectRelatedRegressTests(TestCase):
@ -173,3 +173,19 @@ class SelectRelatedRegressTests(TestCase):
self.assertEqual(Chick.objects.all()[0].mother.name, 'Hen') self.assertEqual(Chick.objects.all()[0].mother.name, 'Hen')
self.assertEqual(Chick.objects.select_related()[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')