#22508: select_related on a foreign related object fails to load fields on original object ----------------------------------------------+-------------------- Reporter: boxm | Owner: nobody Type: Uncategorized | Status: new Component: Database layer (models, ORM) | Version: 1.6 Severity: Normal | Keywords: Triage Stage: Unreviewed | Has patch: 0 Easy pickings: 0 | UI/UX: 0 ----------------------------------------------+-------------------- 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. -- Ticket URL: <https://code.djangoproject.com/ticket/22508> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To post to this group, send email to django-updates@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/047.d78e39567c4b8a35fb3c70abe2919202%40djangoproject.com. For more options, visit https://groups.google.com/d/optout.