From 0f272629ca18e440aef67b4a3fd9377a57fb25a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anssi=20K=C3=A4=C3=A4ri=C3=A4inen?= Date: Sun, 17 Nov 2013 15:50:14 +0200 Subject: [PATCH] [1.6.x] Fixed #21413 -- resolve_columns fields misalignment Backpatch of 9918c11114ac3ec9622631558ef26ebf3919cb69 from master. Conflicts: django/db/models/sql/compiler.py tests/model_inheritance_regress/tests.py --- django/contrib/gis/db/models/sql/compiler.py | 2 +- django/db/models/sql/compiler.py | 15 ++++++++------- tests/model_inheritance_regress/tests.py | 6 ++++++ 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/django/contrib/gis/db/models/sql/compiler.py b/django/contrib/gis/db/models/sql/compiler.py index b89df0ae95c..7b5a309916a 100644 --- a/django/contrib/gis/db/models/sql/compiler.py +++ b/django/contrib/gis/db/models/sql/compiler.py @@ -132,7 +132,7 @@ class GeoSQLCompiler(compiler.SQLCompiler): if table in only_load and field.column not in only_load[table]: continue if as_pairs: - result.append((alias, field.column)) + result.append((alias, field)) aliases.add(alias) continue # This part of the function is customized for GeoQuery. We diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py index a3e7de3788c..0ab53ecc71e 100644 --- a/django/db/models/sql/compiler.py +++ b/django/db/models/sql/compiler.py @@ -289,7 +289,7 @@ class SQLCompiler(object): if table in only_load and field.column not in only_load[table]: continue if as_pairs: - result.append((alias, field.column)) + result.append((alias, field)) aliases.add(alias) continue if with_aliases and field.column in col_aliases: @@ -650,10 +650,10 @@ class SQLCompiler(object): _, _, _, joins, _ = self.query.setup_joins( [f.name], opts, root_alias, outer_if_first=promote) alias = joins[-1] - columns, aliases = self.get_default_columns(start_alias=alias, + columns, _ = self.get_default_columns(start_alias=alias, opts=f.rel.to._meta, as_pairs=True) self.query.related_select_cols.extend( - SelectInfo(col, field) for col, field in zip(columns, f.rel.to._meta.concrete_fields)) + SelectInfo((col[0], col[1].column), col[1]) for col in columns) if restricted: next = requested.get(f.name, {}) else: @@ -678,11 +678,10 @@ class SQLCompiler(object): alias = joins[-1] from_parent = (opts.model if issubclass(model, opts.model) else None) - columns, aliases = self.get_default_columns(start_alias=alias, + columns, _ = self.get_default_columns(start_alias=alias, opts=model._meta, as_pairs=True, from_parent=from_parent) self.query.related_select_cols.extend( - SelectInfo(col, field) for col, field - in zip(columns, model._meta.concrete_fields)) + SelectInfo((col[0], col[1].column), col[1]) for col in columns) next = requested.get(f.related_query_name(), {}) # Use True here because we are looking at the _reverse_ side of # the relation, which is always nullable. @@ -728,8 +727,10 @@ class SQLCompiler(object): # found in get_columns(). It would be nice to clean this up. if self.query.select: fields = [f.field for f in self.query.select] - else: + elif self.query.default_cols: fields = self.query.get_meta().concrete_fields + else: + fields = [] fields = fields + [f.field for f in self.query.related_select_cols] # If the field was deferred, exclude it from being passed diff --git a/tests/model_inheritance_regress/tests.py b/tests/model_inheritance_regress/tests.py index 28635a29a95..54a9c1a0e14 100644 --- a/tests/model_inheritance_regress/tests.py +++ b/tests/model_inheritance_regress/tests.py @@ -441,3 +441,9 @@ class ModelInheritanceTest(TestCase): # used in the qs and top contains direct pointer to the bottom model. qs = ItalianRestaurant.objects.values_list('serves_gnocchi').filter(name='foo') self.assertEqual(str(qs.query).count('JOIN'), 1) + + def test_inheritance_resolve_columns(self): + Restaurant.objects.create(name='Bobs Cafe', address="Somewhere", + serves_pizza=True, serves_hot_dogs=True) + p = Place.objects.all().select_related('restaurant')[0] + self.assertIsInstance(p.restaurant.serves_pizza, bool)