Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #23313 -- Corrected repr(RawQuery) with dict parameters.

  • Loading branch information...
commit bc06d2c11c32bde94b717a75bd3325ef1b94c902 1 parent 909015a
@movEAX movEAX authored timgraham committed
View
5 django/db/models/query.py
@@ -1596,10 +1596,7 @@ def __iter__(self):
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 "<RawQuerySet: %r>" % text
+ return "<RawQuerySet: %s>" % self.query
def __getitem__(self, k):
return list(self)[k]
View
8 django/db/models/sql/query.py
@@ -7,7 +7,7 @@
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 @@ def __iter__(self):
return iter(result)
def __repr__(self):
- return "<RawQuery: %r>" % (self.sql % tuple(self.params))
+ return "<RawQuery: %s>" % 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()
View
6 tests/queries/tests.py
@@ -13,7 +13,7 @@
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 @@ def test_ticket14729(self):
query = "SELECT * FROM queries_note WHERE note = %s"
params = ['n1']
qs = Note.objects.raw(query, params=params)
- self.assertEqual(repr(qs), str_prefix("<RawQuerySet: %(_)s'SELECT * FROM queries_note WHERE note = n1'>"))
+ self.assertEqual(repr(qs), "<RawQuerySet: SELECT * FROM queries_note WHERE note = n1>")
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("<RawQuerySet: %(_)s'SELECT * FROM queries_note WHERE note = n1 and misc = foo'>"))
+ self.assertEqual(repr(qs), "<RawQuerySet: SELECT * FROM queries_note WHERE note = n1 and misc = foo>")
class GeneratorExpressionTests(TestCase):
View
14 tests/raw_query/tests.py
@@ -149,6 +149,20 @@ def test_pyformat_params(self):
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), "<RawQuerySet: SELECT * FROM raw_query_author WHERE last_name = foo>")
+ self.assertEqual(repr(qset.query), "<RawQuery: SELECT * FROM raw_query_author WHERE last_name = foo>")
+
+ query = "SELECT * FROM raw_query_author WHERE last_name = %s"
+ qset = Author.objects.raw(query, {'foo'})
+ self.assertEqual(repr(qset), "<RawQuerySet: SELECT * FROM raw_query_author WHERE last_name = foo>")
+ self.assertEqual(repr(qset.query), "<RawQuery: SELECT * FROM raw_query_author WHERE last_name = foo>")
+
def test_many_to_many(self):
"""
Test of a simple raw query against a model containing a m2m field
Please sign in to comment.
Something went wrong with that request. Please try again.