﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
22508	select_related on a foreign related object fails to load fields on original object	Malcolm Box	Aymeric Augustin	"Consider these models:

{{{
class Show(models.Model):
    pass


class Poll(models.Model):
    event = models.ForeignKey('Event')


class Event(models.Model)
    show = models.ForeignKey(Show)
}}}

The following code fails to load the poll.event.show attribute:

{{{
In [3]: event = Event.objects.all()[2]

In [4]: poll = event.poll_set.select_related('event', 'event__show').all()[0]

In [5]: hasattr(poll.event, '_show_cache')
Out[5]: False

}}}

For comparison, in 1.4 the same code produces:

{{{
In [12]: event = Event.objects.all()[2]

In [13]: poll = event.poll_set.select_related('event', 'event__show').all()[0]

In [14]: hasattr(poll.event, '_show_cache')
Out[14]: True
}}}

The bug appears to be in the known_objects code around line 247 in db/models/query.py which looks like it will overwrite the poll.event attribute that was just retrieved from the DB (via select_related) and replaces it with the parent model, which in this case doesn't have the show attribute loaded.

Since the ORM was explicitly instructed to load the Event model in the call, it shouldn't then replace the poll.event attribute with the previously-known parent model.
"	Bug	closed	Database layer (models, ORM)	1.6	Normal	fixed			Ready for checkin	1	0	0	0	0	0
