3

I got the error below in django when saving a field with emoji character in the admin panel.

OperationalError at /admin/core/message/add/ (1366, "Incorrect string value: '\xF0\x9F\x98\x9E \xF0...' for column 'name' at row 1")

I'm sure the database is ready for utf8mb4, because I can write/read these emoji character in phpmyadmin.

More, the saved emoji character displayed correctly in phpmyadmin but displays ??? in django output.

And in my another django project, the emoji plays well, till I just cannot find the difference between the two environment.

So what can be the problem when I save the same thing using python?

The problem is under django framework, so I want a solution that make django work.

Alfred Huang
  • 17,654
  • 32
  • 118
  • 189
  • Possible duplicate of [MySQL utf8mb4, Errors when saving Emojis](http://stackoverflow.com/questions/35125933/mysql-utf8mb4-errors-when-saving-emojis) – pvg Dec 24 '16 at 08:13

1 Answers1

5

Setting DATABASE section in the settings.py with OPTIONS - charset solves this issue:

# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases

DATABASES = { 
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'myproject',
        'HOST': 'mysql',
        'USER': 'django',
        'PASSWORD': '******',
        'OPTIONS': {
            # !!!!!! THIS MATTERS !!!!!!
            'charset': 'utf8mb4',
        }
    },  
}

See documentation: https://docs.djangoproject.com/en/1.10/ref/settings/#charset

Alfred Huang
  • 17,654
  • 32
  • 118
  • 189