diff options
| author | Russell Keith-Magee <russell@keith-magee.com> | 2010-04-30 16:37:54 +0000 |
|---|---|---|
| committer | Russell Keith-Magee <russell@keith-magee.com> | 2010-04-30 16:37:54 +0000 |
| commit | fe3b75e2764caf3fe7038fc30644e01f14cfc247 (patch) | |
| tree | e58838ac4181836021c2d627a713ed42cc680372 | |
| parent | 868acb3a6c39b73389511f9d65e8849f27e63755 (diff) | |
[1.1.X] Fixed #12851 -- Another attempt at fixing select_related() with inherited models, this time with only(). Thanks to phxx for the test case.
Backport of r13059 from trunk.
git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@13060 bcc190cf-cafb-0310-a4f2-bffc1f526a37
| -rw-r--r-- | django/db/models/query.py | 12 | ||||
| -rw-r--r-- | django/db/models/sql/query.py | 4 | ||||
| -rw-r--r-- | tests/regressiontests/select_related_regress/models.py | 9 |
3 files changed, 22 insertions, 3 deletions
diff --git a/django/db/models/query.py b/django/db/models/query.py index 5c81b8d5fd..36bf6398ba 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -970,7 +970,17 @@ def get_cached_row(klass, row, index_start, max_depth=0, cur_depth=0, return None restricted = requested is not None - load_fields = only_load and only_load.get(klass) or None + if only_load: + load_fields = only_load.get(klass) + # When we create the object, we will also be creating populating + # all the parent classes, so traverse the parent classes looking + # for fields that must be included on load. + for parent in klass._meta.get_parent_list(): + fields = only_load.get(parent) + if fields: + load_fields.update(fields) + else: + load_fields = None if load_fields: # Handle deferred fields. skip = set() diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index b677169f39..fa339262eb 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -621,10 +621,10 @@ class BaseQuery(object): # models. workset = {} for model, values in seen.iteritems(): - for field in model._meta.fields: + for field, m in model._meta.get_fields_with_model(): if field in values: continue - add_to_dict(workset, model, field) + add_to_dict(workset, m or model, field) for model, values in must_include.iteritems(): # If we haven't included a model in workset, we don't add the # corresponding must_include fields for that model, since an diff --git a/tests/regressiontests/select_related_regress/models.py b/tests/regressiontests/select_related_regress/models.py index c99bee0f7e..a673809369 100644 --- a/tests/regressiontests/select_related_regress/models.py +++ b/tests/regressiontests/select_related_regress/models.py @@ -197,4 +197,13 @@ u'Troy Buswell' >>> troy.state.name u'Western Australia' +# Also works if you use only, rather than defer +>>> troy = SpecialClient.objects.select_related('state').only('name').get(name='Troy Buswell') +>>> troy.name +u'Troy Buswell' +>>> troy.value +42 +>>> troy.state.name +u'Western Australia' + """} |
