| | 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 |