Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #12851 -- Another attempt at fixing select_related() with inher…

…ited models, this time with only(). Thanks to phxx for the test case.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@13059 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 8b1aebbf352cd2c7e0c3bd81b83553fe293ad56a 1 parent 21d98c5
Russell Keith-Magee authored April 30, 2010
12  django/db/models/query.py
@@ -1154,7 +1154,17 @@ def get_cached_row(klass, row, index_start, using, max_depth=0, cur_depth=0,
1154 1154
         return None
1155 1155
 
1156 1156
     restricted = requested is not None
1157  
-    load_fields = only_load and only_load.get(klass) or None
  1157
+    if only_load:
  1158
+        load_fields = only_load.get(klass)
  1159
+        # When we create the object, we will also be creating populating
  1160
+        # all the parent classes, so traverse the parent classes looking
  1161
+        # for fields that must be included on load.
  1162
+        for parent in klass._meta.get_parent_list():
  1163
+            fields = only_load.get(parent)
  1164
+            if fields:
  1165
+                load_fields.update(fields)
  1166
+    else:
  1167
+        load_fields = None
1158 1168
     if load_fields:
1159 1169
         # Handle deferred fields.
1160 1170
         skip = set()
4  django/db/models/sql/query.py
@@ -556,10 +556,10 @@ def deferred_to_data(self, target, callback):
556 556
             # models.
557 557
             workset = {}
558 558
             for model, values in seen.iteritems():
559  
-                for field in model._meta.fields:
  559
+                for field, m in model._meta.get_fields_with_model():
560 560
                     if field in values:
561 561
                         continue
562  
-                    add_to_dict(workset, model, field)
  562
+                    add_to_dict(workset, m or model, field)
563 563
             for model, values in must_include.iteritems():
564 564
                 # If we haven't included a model in workset, we don't add the
565 565
                 # corresponding must_include fields for that model, since an
9  tests/regressiontests/select_related_regress/models.py
@@ -197,4 +197,13 @@ def __unicode__(self):

0 notes on commit 8b1aebb

Please sign in to comment.
Something went wrong with that request. Please try again.