Setting up a Django / MySQL site to use UTF-8

I want my Django site to use UTF-8 for MySQL without error, regardless of whether MySQL uses the default UTF-8 installation. In addition to creating UTF-8 encoded tables, I added the following database initialization to settings.py to make sure the connection also uses utf-8:

'OPTIONS': { 'init_command': 'SET storage_engine=INNODB; SET names "utf8"' } 

This will result in an error:

 _mysql_exceptions.ProgrammingError: (2014, "Commands out of sync; you can't run this command now") 

What is the right way to do this? Is there another place where SET NAMES should be executed?

+7
source share
3 answers

You only need to use SET once and add other commands like:

 'SET storage_engine=INNODB,character_set_connection=utf8,collation_connection=utf8_unicode_ci' 
+11
source

All I had to do was put this in settings.py :

 'OPTIONS': { 'init_command': 'SET storage_engine=INNODB' } 

Then I created the database myself in MySQL using

 CREATE DATABASE my_database CHARACTER SET utf8; 

followed by all calls to CREATE USER and GRANT .

After that ./manage.py syncdb and ./manage.py migrate (since I use South) finished everything.

So far, it has worked fine. There was no need to change the MySQL configuration files. (Sysadmin wanted to keep MySQL using the default latin1 encoding for other users.)

+10
source

Note: http://blog.ionelmc.ro/2014/12/28/terrible-choices-mysql/

  'SET ' 'storage_engine=INNODB,' 'character_set_connection=utf8,' 'collation_connection=utf8_bin,' 'SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED', } 
+3
source

All Articles