Commit f7cf58ac authored by Russell Keith-Magee's avatar Russell Keith-Magee
Browse files

Fixed #12429 -- Ensure that raw queries call resolve_columns if the backend...

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
parent c39ec6dc
Loading
Loading
Loading
Loading
+9 −2
Original line number Diff line number Diff line
@@ -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),

+0 −4
Original line number Diff line number Diff line
@@ -14,10 +14,6 @@ class SQLCompiler(object):
        self.using = using
        self.quote_cache = {}

        # Check that the compiler will be able to execute the query
        for alias, aggregate in self.query.aggregate_select.items():
            self.connection.ops.check_aggregate_support(aggregate)

    def pre_sql_setup(self):
        """
        Does any necessary class setup immediately prior to producing SQL. This
+5 −0
Original line number Diff line number Diff line
@@ -189,6 +189,11 @@ class Query(object):
            raise ValueError("Need either using or connection")
        if using:
            connection = connections[using]

        # Check that the compiler will be able to execute the query
        for alias, aggregate in self.aggregate_select.items():
            connection.ops.check_aggregate_support(aggregate)

        return connection.ops.compiler(self.compiler)(self, connection, using)

    def get_meta(self):
+12 −4
Original line number Diff line number Diff line
@@ -40,7 +40,9 @@
        "model": "raw_query.book",
        "fields": {
            "author": 1,
            "title": "The awesome book"
            "title": "The awesome book",
            "paperback": false,
            "opening_line": "It was a bright cold day in April and the clocks were striking thirteen."
        }
    },
    {
@@ -48,7 +50,9 @@
        "model": "raw_query.book",
        "fields": {
            "author": 1,
            "title": "The horrible book"
            "title": "The horrible book",
            "paperback": true,
            "opening_line": "On an evening in the latter part of May a middle-aged man was walking homeward from Shaston to the village of Marlott, in the adjoining Vale of Blakemore, or Blackmoor."
        }
    },
    {
@@ -56,7 +60,9 @@
        "model": "raw_query.book",
        "fields": {
            "author": 1,
            "title": "Another awesome book"
            "title": "Another awesome book",
            "paperback": false,
            "opening_line": "A squat grey building of only thirty-four stories."
        }
    },
    {
@@ -64,7 +70,9 @@
        "model": "raw_query.book",
        "fields": {
            "author": 3,
            "title": "Some other book"
            "title": "Some other book",
            "paperback": true,
            "opening_line": "It was the day my grandmother exploded."
        }
    },
    {
+2 −0
Original line number Diff line number Diff line
@@ -17,6 +17,8 @@ class Author(models.Model):
class Book(models.Model):
    title = models.CharField(max_length=255)
    author = models.ForeignKey(Author)
    paperback = models.BooleanField()
    opening_line = models.TextField()

class Coffee(models.Model):
    brand = models.CharField(max_length=255, db_column="name")
Loading