Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.1.X] Fixed #12851 -- Another attempt at fixing select_related() wi…

…th 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
  • Loading branch information...
commit fe3b75e2764caf3fe7038fc30644e01f14cfc247 1 parent 868acb3
Russell Keith-Magee authored
12  django/db/models/query.py
@@ -970,7 +970,17 @@ def get_cached_row(klass, row, index_start, max_depth=0, cur_depth=0,
970 970
         return None
971 971
 
972 972
     restricted = requested is not None
973  
-    load_fields = only_load and only_load.get(klass) or None
  973
+    if only_load:
  974
+        load_fields = only_load.get(klass)
  975
+        # When we create the object, we will also be creating populating
  976
+        # all the parent classes, so traverse the parent classes looking
  977
+        # for fields that must be included on load.
  978
+        for parent in klass._meta.get_parent_list():
  979
+            fields = only_load.get(parent)
  980
+            if fields:
  981
+                load_fields.update(fields)
  982
+    else:
  983
+        load_fields = None
974 984
     if load_fields:
975 985
         # Handle deferred fields.
976 986
         skip = set()
4  django/db/models/sql/query.py
@@ -621,10 +621,10 @@ def deferred_to_data(self, target, callback):
621 621
             # models.
622 622
             workset = {}
623 623
             for model, values in seen.iteritems():
624  
-                for field in model._meta.fields:
  624
+                for field, m in model._meta.get_fields_with_model():
625 625
                     if field in values:
626 626
                         continue
627  
-                    add_to_dict(workset, model, field)
  627
+                    add_to_dict(workset, m or model, field)
628 628
             for model, values in must_include.iteritems():
629 629
                 # If we haven't included a model in workset, we don't add the
630 630
                 # 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 fe3b75e

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