Ticket #16043: 16043-testcase.patch
File 16043-testcase.patch, 2.1 KB (added by , 13 years ago) |
---|
-
modeltests/select_related/tests.py
1 from __future__ import with_statement 2 1 3 from django.test import TestCase 2 4 3 5 from models import Domain, Kingdom, Phylum, Klass, Order, Family, Genus, Species 6 from models import KingdomOnPlanet 4 7 5 8 class SelectRelatedTests(TestCase): 6 9 … … 166 169 Species.objects.select_related, 167 170 'genus__family__order', depth=4 168 171 ) 172 173 def test_issue_16043(self): 174 """ 175 Regression test for #16043. 176 If the parent class has a foreign key to another object, make sure 177 that the target of the foreign key is cached for both the parent and 178 the child. 179 """ 180 related = Domain.objects.create(name='Eukaryota') 181 child = KingdomOnPlanet.objects.create(name='Protista', planet='Earth', 182 domain=related) 183 # 1 SQL request needed 184 with self.assertNumQueries(1): 185 parent = Kingdom.objects.select_related('domain').get(name='Protista') 186 # no SQL needed, thanks to select_related 187 with self.assertNumQueries(0): 188 parent.domain 189 # 1 SQL request needed to obtain the specialization 190 with self.assertNumQueries(1): 191 child = parent.kingdomonplanet 192 # no SQL needed, the related object was cached at step 2 193 with self.assertNumQueries(0): 194 child.domain -
modeltests/select_related/models.py
56 56 name = models.CharField(max_length=50) 57 57 genus = models.ForeignKey(Genus) 58 58 def __unicode__(self): 59 return self.name 60 No newline at end of file 59 return self.name 60 61 # Sorry, we need some science-fiction too. 62 63 class KingdomOnPlanet(Kingdom): 64 planet = models.CharField(max_length=50)