Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Convert binary-matched VARCHAR fields to unicode objects in the MySQL…

… backend.

This conforms to Djangos' policy of returning Unicode everywhere.

Suggested by arne and Martin von Löwis. Refs #7789.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@8318 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit ca71eacdf431c341a799c424328db835a949891e 1 parent 6bebb23
Malcolm Tredinnick authored

Showing 1 changed file with 10 additions and 3 deletions. Show diff stats Hide diff stats

  1. 13  django/db/backends/mysql/base.py
13  django/db/backends/mysql/base.py
@@ -4,6 +4,8 @@
4 4
 Requires MySQLdb: http://sourceforge.net/projects/mysql-python
5 5
 """
6 6
 
  7
+import re
  8
+
7 9
 from django.db.backends import *
8 10
 from django.db.backends.mysql.client import DatabaseClient
9 11
 from django.db.backends.mysql.creation import DatabaseCreation
@@ -26,8 +28,7 @@
26 28
     raise ImproperlyConfigured("MySQLdb-1.2.1p2 or newer is required; you have %s" % Database.__version__)
27 29
 
28 30
 from MySQLdb.converters import conversions
29  
-from MySQLdb.constants import FIELD_TYPE
30  
-import re
  31
+from MySQLdb.constants import FIELD_TYPE, FLAG
31 32
 
32 33
 # Raise exceptions for database warnings if DEBUG is on
33 34
 from django.conf import settings
@@ -49,6 +50,12 @@
49 50
     FIELD_TYPE.TIME: util.typecast_time,
50 51
     FIELD_TYPE.DECIMAL: util.typecast_decimal,
51 52
     FIELD_TYPE.NEWDECIMAL: util.typecast_decimal,
  53
+    # By default, mysqldb will return VARCHAR BINARY fields as type str.
  54
+    # This is a bad idea, as BINARY doesn't indicate that it's arbitrary
  55
+    # binary data, but that collation uses the binary representation.
  56
+    # Replacing the list makes it return unicode. MySQLdb later adds
  57
+    # another list entry for non-binary fields.
  58
+    FIELD_TYPE.VARCHAR: [(FLAG.BINARY, lambda s: s.decode('utf-8'))],
52 59
 })
53 60
 
54 61
 # This should match the numerical portion of the version numbers (we can treat
@@ -167,7 +174,7 @@ class DatabaseWrapper(BaseDatabaseWrapper):
167 174
     def __init__(self, **kwargs):
168 175
         super(DatabaseWrapper, self).__init__(**kwargs)
169 176
         self.server_version = None
170  
-        
  177
+
171 178
         self.features = DatabaseFeatures()
172 179
         self.ops = DatabaseOperations()
173 180
         self.client = DatabaseClient()

0 notes on commit ca71eac

Please sign in to comment.
Something went wrong with that request. Please try again.