Fixed #12429 -- Ensure that raw queries call resolve_columns if the backend defines it. This ensures (as much as possible) that the model values returned by a raw query match that in normal queries. Thanks to Ian Kelly for the report.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12904 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Russell Keith-Magee 2010-04-01 16:48:16 +00:00
parent c39ec6dccb
commit f7cf58ac0e
6 changed files with 39 additions and 14 deletions

View file

@ -1407,20 +1407,27 @@ class RawQuerySet(object):
self._model_fields = {}
for field in self.model._meta.fields:
name, column = field.get_attname_column()
self._model_fields[converter(column)] = name
self._model_fields[converter(column)] = field
return self._model_fields
def transform_results(self, values):
model_init_kwargs = {}
annotations = ()
# Perform database backend type resolution
connection = connections[self.db]
compiler = connection.ops.compiler('SQLCompiler')(self.query, connection, self.db)
if hasattr(compiler, 'resolve_columns'):
fields = [self.model_fields.get(c,None) for c in self.columns]
values = compiler.resolve_columns(values, fields)
# Associate fields to values
for pos, value in enumerate(values):
column = self.columns[pos]
# Separate properties from annotations
if column in self.model_fields.keys():
model_init_kwargs[self.model_fields[column]] = value
model_init_kwargs[self.model_fields[column].attname] = value
else:
annotations += (column, value),