Skip to content

Commit

Permalink
Fixed #22508 -- Avoided overwriting select_related.
Browse files Browse the repository at this point in the history
Previously, known related objects overwrote related objects loaded
though select_related. This could cancel the effect of select_related
when it was used over more than one level.

Thanks boxm for the bug report and timo for bisecting the regression.
  • Loading branch information
aaugustin committed May 10, 2014
1 parent fb90b7c commit f574220
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions django/db/models/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,9 @@ def iterator(self):
# Add the known related objects to the model, if there are any
if self._known_related_objects:
for field, rel_objs in self._known_related_objects.items():
# Avoid overwriting objects loaded e.g. by select_related
if hasattr(obj, field.get_cache_name()):
continue
pk = getattr(obj, field.get_attname())
try:
rel_obj = rel_objs[pk]
Expand Down
10 changes: 10 additions & 0 deletions tests/select_related_regress/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,13 @@ def test_ticket_10733(self):
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')

def test_regression_22508(self):
building = Building.objects.create(name='101')
device = Device.objects.create(name="router", building=building)
Port.objects.create(port_number='1', device=device)

device = Device.objects.get()
port = device.port_set.select_related('device__building').get()
with self.assertNumQueries(0):
port.device.building

0 comments on commit f574220

Please sign in to comment.