Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #7246 -- Pull in the all the necessary data when using select_r…

…elated() with multi-table inheritance.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@7781 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit a8fa3fd81fee316144986328f3df1e811015f1d9 1 parent b67164f
Malcolm Tredinnick authored
27  django/db/models/sql/query.py
@@ -442,28 +442,39 @@ def get_columns(self, with_aliases=False):
442 442
         self._select_aliases = aliases
443 443
         return result
444 444
 
445  
-    def get_default_columns(self, with_aliases=False, col_aliases=None):
  445
+    def get_default_columns(self, with_aliases=False, col_aliases=None,
  446
+            start_alias=None, opts=None, as_pairs=False):
446 447
         """
447 448
         Computes the default columns for selecting every field in the base
448 449
         model.
449 450
 
450 451
         Returns a list of strings, quoted appropriately for use in SQL
451  
-        directly, as well as a set of aliases used in the select statement.
  452
+        directly, as well as a set of aliases used in the select statement (if
  453
+        'as_pairs' is True, returns a list of (alias, col_name) pairs instead
  454
+        of strings as the first component and None as the second component).
452 455
         """
453 456
         result = []
454  
-        table_alias = self.tables[0]
455  
-        root_pk = self.model._meta.pk.column
  457
+        if opts is None:
  458
+            opts = self.model._meta
  459
+        if start_alias:
  460
+            table_alias = start_alias
  461
+        else:
  462
+            table_alias = self.tables[0]
  463
+        root_pk = opts.pk.column
456 464
         seen = {None: table_alias}
457 465
         qn = self.quote_name_unless_alias
458 466
         qn2 = self.connection.ops.quote_name
459 467
         aliases = set()
460  
-        for field, model in self.model._meta.get_fields_with_model():
  468
+        for field, model in opts.get_fields_with_model():
461 469
             try:
462 470
                 alias = seen[model]
463 471
             except KeyError:
464 472
                 alias = self.join((table_alias, model._meta.db_table,
465 473
                         root_pk, model._meta.pk.column))
466 474
                 seen[model] = alias
  475
+            if as_pairs:
  476
+                result.append((alias, field.column))
  477
+                continue
467 478
             if with_aliases and field.column in col_aliases:
468 479
                 c_alias = 'Col%d' % len(col_aliases)
469 480
                 result.append('%s.%s AS %s' % (qn(alias),
@@ -476,6 +487,8 @@ def get_default_columns(self, with_aliases=False, col_aliases=None):
476 487
                 aliases.add(r)
477 488
                 if with_aliases:
478 489
                     col_aliases.add(field.column)
  490
+        if as_pairs:
  491
+            return result, None
479 492
         return result, aliases
480 493
 
481 494
     def get_from_clause(self):
@@ -941,8 +954,8 @@ def fill_related_selections(self, opts=None, root_alias=None, cur_depth=1,
941 954
                     f.rel.get_related_field().column), exclusions=used,
942 955
                     promote=promote)
943 956
             used.add(alias)
944  
-            self.related_select_cols.extend([(alias, f2.column)
945  
-                    for f2 in f.rel.to._meta.fields])
  957
+            self.related_select_cols.extend(self.get_default_columns(
  958
+                start_alias=alias, opts=f.rel.to._meta, as_pairs=True)[0])
946 959
             self.related_select_fields.extend(f.rel.to._meta.fields)
947 960
             if restricted:
948 961
                 next = requested.get(f.name, {})
0  tests/regressiontests/model_inheritance_select_related/__init__.py
No changes.
48  tests/regressiontests/model_inheritance_select_related/models.py
... ...
@@ -0,0 +1,48 @@

0 notes on commit a8fa3fd

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