From 3a5dae5ed3ee10d7c86dc7a110f1edb60490cd99 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Sat, 20 Mar 2010 15:37:26 +0000 Subject: [PATCH] Fixed #12851 -- Corrected the interaction of defer() with select_related(). Thanks to ruosteinen for the report. git-svn-id: http://code.djangoproject.com/svn/django/trunk@12817 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/query.py | 4 ++-- tests/regressiontests/defer_regress/models.py | 2 +- tests/regressiontests/select_related_regress/models.py | 9 +++++++++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/django/db/models/query.py b/django/db/models/query.py index 8adf0d555c..d0b605db84 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -1208,7 +1208,7 @@ def get_cached_row(klass, row, index_start, using, max_depth=0, cur_depth=0, next = None # Recursively retrieve the data for the related object cached_row = get_cached_row(f.rel.to, row, index_end, using, - max_depth, cur_depth+1, next) + max_depth, cur_depth+1, next, only_load=only_load) # If the recursive descent found an object, populate the # descriptor caches relevant to the object if cached_row: @@ -1237,7 +1237,7 @@ def get_cached_row(klass, row, index_start, using, max_depth=0, cur_depth=0, next = requested[f.related_query_name()] # Recursively retrieve the data for the related object cached_row = get_cached_row(model, row, index_end, using, - max_depth, cur_depth+1, next, local_only=True) + max_depth, cur_depth+1, next, only_load=only_load, local_only=True) # If the recursive descent found an object, populate the # descriptor caches relevant to the object if cached_row: diff --git a/tests/regressiontests/defer_regress/models.py b/tests/regressiontests/defer_regress/models.py index 08c4d621b6..8b42165891 100644 --- a/tests/regressiontests/defer_regress/models.py +++ b/tests/regressiontests/defer_regress/models.py @@ -142,7 +142,7 @@ False [, , , , ] >>> sorted(get_models(models.get_app('defer_regress'), include_deferred=True), key=lambda obj: obj._meta.object_name) -[, , , , , , , , , , , , , , , , ] +[, , , , , , , , , , , , , , , , , ] """ } diff --git a/tests/regressiontests/select_related_regress/models.py b/tests/regressiontests/select_related_regress/models.py index b2664f9398..9eaf934777 100644 --- a/tests/regressiontests/select_related_regress/models.py +++ b/tests/regressiontests/select_related_regress/models.py @@ -165,4 +165,13 @@ Exercising select_related() with multi-table model inheritance. >>> Item.objects.select_related("child").order_by("name") [, ] +# Regression for #12851 - Deferred fields are used correctly if you +# select_related a subset of fields. +>>> wa = State.objects.create(name="Western Australia", country=australia) +>>> _ = Client.objects.create(name='Brian Burke', state=wa, status=active) +>>> burke = Client.objects.select_related('state').defer('state__name').get(name='Brian Burke') +>>> burke.state.name +u'Western Australia' + """} +