From 9918c11114ac3ec9622631558ef26ebf3919cb69 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] Fixed #21413 -- resolve_columns fields misalignment --- 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 21fb57a417..4b4a5bab45 100644 --- a/django/contrib/gis/db/models/sql/compiler.py +++ b/django/contrib/gis/db/models/sql/compiler.py @@ -133,7 +133,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 54d0063b9e..41bba93206 100644 --- a/django/db/models/sql/compiler.py +++ b/django/db/models/sql/compiler.py @@ -295,7 +295,7 @@ class SQLCompiler(object): if table in only_load and column not in only_load[table]: continue if as_pairs: - result.append((alias, column)) + result.append((alias, field)) aliases.add(alias) continue if with_aliases and column in col_aliases: @@ -625,10 +625,10 @@ class SQLCompiler(object): _, _, _, joins, _ = self.query.setup_joins( [f.name], opts, root_alias) 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: @@ -652,11 +652,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(), {}) self.fill_related_selections(model._meta, alias, cur_depth + 1, next, restricted) @@ -699,8 +698,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 e816fe6bc7..211877d7c6 100644 --- a/tests/model_inheritance_regress/tests.py +++ b/tests/model_inheritance_regress/tests.py @@ -460,3 +460,9 @@ class ModelInheritanceTest(TestCase): ) Senator.objects.get(pk=senator.pk) + + 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)