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

This commit is contained in:
Andrew Torpedov 2014-08-17 14:21:48 +00:00 committed by Tim Graham
parent 909015a51b
commit bc06d2c11c
4 changed files with 24 additions and 9 deletions

View File

@ -1596,10 +1596,7 @@ class RawQuerySet(object):
self.query.cursor.close() self.query.cursor.close()
def __repr__(self): def __repr__(self):
text = self.raw_query return "<RawQuerySet: %s>" % self.query
if self.params:
text = text % (self.params if hasattr(self.params, 'keys') else tuple(self.params))
return "<RawQuerySet: %r>" % text
def __getitem__(self, k): def __getitem__(self, k):
return list(self)[k] return list(self)[k]

View File

@ -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. 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 copy
import warnings import warnings
@ -83,7 +83,11 @@ class RawQuery(object):
return iter(result) return iter(result)
def __repr__(self): 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): def _execute_query(self):
self.cursor = connections[self.using].cursor() self.cursor = connections[self.using].cursor()

View File

@ -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.where import WhereNode, EverythingNode, NothingNode
from django.db.models.sql.datastructures import EmptyResultSet from django.db.models.sql.datastructures import EmptyResultSet
from django.test import TestCase, skipUnlessDBFeature 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.deprecation import RemovedInDjango19Warning
from django.utils import six from django.utils import six
@ -1846,12 +1846,12 @@ class RawQueriesTests(TestCase):
query = "SELECT * FROM queries_note WHERE note = %s" query = "SELECT * FROM queries_note WHERE note = %s"
params = ['n1'] params = ['n1']
qs = Note.objects.raw(query, params=params) 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" query = "SELECT * FROM queries_note WHERE note = %s and misc = %s"
params = ['n1', 'foo'] params = ['n1', 'foo']
qs = Note.objects.raw(query, params=params) 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): class GeneratorExpressionTests(TestCase):

View File

@ -149,6 +149,20 @@ class RawQueryTests(TestCase):
self.assertEqual(len(results), 1) self.assertEqual(len(results), 1)
self.assertIsInstance(repr(qset), str) 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): def test_many_to_many(self):
""" """
Test of a simple raw query against a model containing a m2m field Test of a simple raw query against a model containing a m2m field