From 2b1934ff3c3f80f58cc35bdcc03ae1b0ea6eb17e Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Sat, 4 Apr 2009 05:35:01 +0000 Subject: [PATCH] Fixed a problem when computing deferred fields on multiple related models. Fixed #10710, as this fixes the second bug reported there. git-svn-id: http://code.djangoproject.com/svn/django/trunk@10384 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/sql/query.py | 7 ++++--- tests/regressiontests/defer_regress/models.py | 3 +++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index 4735fdabbb..cafde11415 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -574,12 +574,13 @@ class BaseQuery(object): if not field_names: return columns = set() - cur_model = self.model - opts = cur_model._meta + orig_opts = self.model._meta seen = {} - must_include = {cur_model: set([opts.pk])} + must_include = {self.model: set([orig_opts.pk])} for field_name in field_names: parts = field_name.split(LOOKUP_SEP) + cur_model = self.model + opts = orig_opts for name in parts[:-1]: old_model = cur_model source = opts.get_field_by_name(name)[0] diff --git a/tests/regressiontests/defer_regress/models.py b/tests/regressiontests/defer_regress/models.py index d7ba713061..e2a5944f92 100644 --- a/tests/regressiontests/defer_regress/models.py +++ b/tests/regressiontests/defer_regress/models.py @@ -24,6 +24,7 @@ class Child(models.Model): class Leaf(models.Model): name = models.CharField(max_length=10) child = models.ForeignKey(Child) + second_child = models.ForeignKey(Child, related_name="other", null=True) value = models.IntegerField(default=42) def __unicode__(self): @@ -87,6 +88,8 @@ Some further checks for select_related() and inherited model behaviour >>> obj = Leaf.objects.only("name", "child").select_related()[0] >>> obj.child.name u'c1' +>>> Leaf.objects.select_related().only("child__name", "second_child__name") +[] """ }