diff --git a/tests/select_related_regress/models.py b/tests/select_related_regress/models.py
index 620b8eb..a243b3b 100644
a
|
b
|
class Item(models.Model):
|
95 | 95 | def __str__(self): |
96 | 96 | return self.name |
97 | 97 | |
| 98 | class ChildA(models.Model): |
| 99 | parent = models.ForeignKey(Parent) |
| 100 | |
| 101 | @python_2_unicode_compatible |
| 102 | class ChildB(models.Model): |
| 103 | name = models.CharField(max_length=10) |
| 104 | parent = models.ForeignKey(Parent, null=True) |
| 105 | child = models.ForeignKey(ChildA, null=True) |
| 106 | |
| 107 | def __str__(self): |
| 108 | return self.name |
| 109 | |
98 | 110 | # Models for testing bug #19870. |
99 | 111 | @python_2_unicode_compatible |
100 | 112 | class Fowl(models.Model): |
diff --git a/tests/select_related_regress/tests.py b/tests/select_related_regress/tests.py
index f6d21b2..078b9d7 100644
a
|
b
|
|
1 | 1 | from __future__ import absolute_import, unicode_literals |
2 | 2 | |
| 3 | from django.db import models |
3 | 4 | from django.test import TestCase |
4 | 5 | from django.utils import six |
5 | 6 | |
6 | 7 | from .models import (Building, Child, Device, Port, Item, Country, Connection, |
7 | 8 | ClientStatus, State, Client, SpecialClient, TUser, Person, Student, |
8 | | Organizer, Class, Enrollment, Hen, Chick) |
| 9 | Organizer, Class, Enrollment, Hen, Chick, Parent, ChildA, ChildB) |
9 | 10 | |
10 | 11 | |
11 | 12 | class SelectRelatedRegressTests(TestCase): |
… |
… |
class SelectRelatedRegressTests(TestCase):
|
173 | 174 | |
174 | 175 | self.assertEqual(Chick.objects.all()[0].mother.name, 'Hen') |
175 | 176 | self.assertEqual(Chick.objects.select_related()[0].mother.name, 'Hen') |
| 177 | |
| 178 | def test_ticket_20528(self): |
| 179 | """ |
| 180 | Regression for #20528 |
| 181 | |
| 182 | Using Qs and select_related with a certain config of ForeignKeys |
| 183 | resulted in incorrect joins. |
| 184 | """ |
| 185 | parent = Parent.objects.create(name="foo") |
| 186 | childA = ChildA.objects.create(parent=parent) |
| 187 | childB = ChildB.objects.create(child=childA, name="Bobby") |
| 188 | queryset = ChildB.objects.filter( |
| 189 | models.Q(parent=parent) | models.Q(child__parent=parent) |
| 190 | ) |
| 191 | expected_queryset = ['<ChildB: Bobby>'] |
| 192 | # this always works |
| 193 | self.assertQuerysetEqual(queryset, expected_queryset) |
| 194 | |
| 195 | # this started failing after 3fef304ff237fe692459c1f5b840acf7886c50bb |
| 196 | queryset = queryset.select_related('parent') |
| 197 | import sys |
| 198 | sys.stderr.write(str(queryset.query)) |
| 199 | self.assertQuerysetEqual(queryset, expected_queryset) |