Code

Ticket #18461: 18461-1.diff

File 18461-1.diff, 2.2 KB (added by claudep, 22 months ago)

Decode returned value from last executed query

Line 
1diff --git a/django/db/backends/mysql/base.py b/django/db/backends/mysql/base.py
2index 9de3287..f381d48 100644
3--- a/django/db/backends/mysql/base.py
4+++ b/django/db/backends/mysql/base.py
5@@ -238,7 +238,7 @@ class DatabaseOperations(BaseDatabaseOperations):
6         # With MySQLdb, cursor objects have an (undocumented) "_last_executed"
7         # attribute where the exact query sent to the database is saved.
8         # See MySQLdb/cursors.py in the source distribution.
9-        return cursor._last_executed
10+        return cursor._last_executed.decode('utf-8')
11 
12     def no_limit_value(self):
13         # 2**64 - 1, as recommended by the MySQL documentation
14diff --git a/django/db/backends/postgresql_psycopg2/operations.py b/django/db/backends/postgresql_psycopg2/operations.py
15index 46b464b..88a413b 100644
16--- a/django/db/backends/postgresql_psycopg2/operations.py
17+++ b/django/db/backends/postgresql_psycopg2/operations.py
18@@ -193,7 +193,7 @@ class DatabaseOperations(BaseDatabaseOperations):
19     def last_executed_query(self, cursor, sql, params):
20         # http://initd.org/psycopg/docs/cursor.html#cursor.query
21         # The query attribute is a Psycopg extension to the DB API 2.0.
22-        return cursor.query
23+        return cursor.query.decode('utf-8')
24 
25     def return_insert_id(self):
26         return "RETURNING %s", ()
27diff --git a/tests/regressiontests/logging_tests/tests.py b/tests/regressiontests/logging_tests/tests.py
28index 4ae3c1b..d443101 100644
29--- a/tests/regressiontests/logging_tests/tests.py
30+++ b/tests/regressiontests/logging_tests/tests.py
31@@ -1,3 +1,4 @@
32+# -*- encoding: utf-8 -*-
33 from __future__ import unicode_literals
34 
35 import copy
36@@ -252,3 +253,13 @@ class AdminEmailHandlerTest(TestCase):
37 
38         self.assertEqual(len(mail.outbox), 1)
39         self.assertEqual(mail.outbox[0].subject, expected_subject)
40+
41+class RequestLoggingTest(TestCase):
42+    @override_settings(DEBUG=True)
43+    def test_sql_logging(self):
44+        """
45+        Test that sql logging of last executed query doesn't produce decoding
46+        error with Unicode caracters (#18461)
47+        """
48+        from django.contrib.auth.models import User
49+        q = list(User.objects.filter(last_name='й'))