[1.6.x] Fixed #21413 -- resolve_columns fields misalignment

Backpatch of 9918c11114 from master.

Conflicts:

	django/db/models/sql/compiler.py
	tests/model_inheritance_regress/tests.py
This commit is contained in:
Anssi Kääriäinen 2013-11-17 15:50:14 +02:00
parent 5f42c02195
commit 0f272629ca
3 changed files with 15 additions and 8 deletions

View File

@ -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

View File

@ -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

View File

@ -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)