diff --git a/django/db/models/query.py b/django/db/models/query.py index 5d51a7c955..aa55b76322 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -1596,10 +1596,7 @@ class RawQuerySet(object): self.query.cursor.close() def __repr__(self): - text = self.raw_query - if self.params: - text = text % (self.params if hasattr(self.params, 'keys') else tuple(self.params)) - return "" % text + return "" % self.query def __getitem__(self, k): return list(self)[k] diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index a590f2796e..9ec84d64cd 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -7,7 +7,7 @@ databases). The abstraction barrier only works one way: this module has to know all about the internals of models in order to get the information it needs. """ -from collections import OrderedDict +from collections import Mapping, OrderedDict import copy import warnings @@ -83,7 +83,11 @@ class RawQuery(object): return iter(result) def __repr__(self): - return "" % (self.sql % tuple(self.params)) + return "" % self + + def __str__(self): + _type = dict if isinstance(self.params, Mapping) else tuple + return self.sql % _type(self.params) def _execute_query(self): self.cursor = connections[self.using].cursor() diff --git a/tests/queries/tests.py b/tests/queries/tests.py index e7dd7fab74..198594ea42 100644 --- a/tests/queries/tests.py +++ b/tests/queries/tests.py @@ -13,7 +13,7 @@ from django.db.models import Count, F, Q from django.db.models.sql.where import WhereNode, EverythingNode, NothingNode from django.db.models.sql.datastructures import EmptyResultSet from django.test import TestCase, skipUnlessDBFeature -from django.test.utils import str_prefix, CaptureQueriesContext +from django.test.utils import CaptureQueriesContext from django.utils.deprecation import RemovedInDjango19Warning from django.utils import six @@ -1846,12 +1846,12 @@ class RawQueriesTests(TestCase): query = "SELECT * FROM queries_note WHERE note = %s" params = ['n1'] qs = Note.objects.raw(query, params=params) - self.assertEqual(repr(qs), str_prefix("")) + self.assertEqual(repr(qs), "") query = "SELECT * FROM queries_note WHERE note = %s and misc = %s" params = ['n1', 'foo'] qs = Note.objects.raw(query, params=params) - self.assertEqual(repr(qs), str_prefix("")) + self.assertEqual(repr(qs), "") class GeneratorExpressionTests(TestCase): diff --git a/tests/raw_query/tests.py b/tests/raw_query/tests.py index 4c2bc211bb..200ffb61e1 100644 --- a/tests/raw_query/tests.py +++ b/tests/raw_query/tests.py @@ -149,6 +149,20 @@ class RawQueryTests(TestCase): self.assertEqual(len(results), 1) self.assertIsInstance(repr(qset), str) + def test_query_representation(self): + """ + Test representation of raw query with parameters + """ + query = "SELECT * FROM raw_query_author WHERE last_name = %(last)s" + qset = Author.objects.raw(query, {'last': 'foo'}) + self.assertEqual(repr(qset), "") + self.assertEqual(repr(qset.query), "") + + query = "SELECT * FROM raw_query_author WHERE last_name = %s" + qset = Author.objects.raw(query, {'foo'}) + self.assertEqual(repr(qset), "") + self.assertEqual(repr(qset.query), "") + def test_many_to_many(self): """ Test of a simple raw query against a model containing a m2m field