Search code examples
mysqldjangounicodeemoji

Entering emoji fails in django + mysql even with utf8mb4_unicode_ci


I'm trying to insert an emoji into my title column in my django + MySQL app.

I edited the database directly with PhpMyAdmin. I changed the charset of the column to utf8mb4_unicode_ci and manually inserted an emoji in the table.

This is the table structure: enter image description here

And this is the table row with an emoji: enter image description here

But when I open this object in the django admin, the emoji is missing:

enter image description here

And if I try to enter the emoji and save, I get this exception:

MySQLdb._exceptions.OperationalError: (1366, "Incorrect string value: '\\\\xF0\\\\x9F\\\\x92\\\\xA9' for column 'title' at row 1")

What am I missing?


Solution

  • What's missing is the charset option for the mysql database

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            ...
            'OPTIONS': {
                ...
                'charset': 'utf8mb4',
            },
        }
    }