1

I have some code (at django server) that writes json data contained in POST requests into mysql db. Some requests contain specific pictograms like or . Those are supposed to be written to a text field, but cause a db error.

Code processing the requests looks like

            json_data = json.loads(request.body.decode('utf-8'))
            i = Event(event=json_data['event'], dt=parse(json_data['dt']),
                      object_id=json_data['object_id'], user_id=json_data['user_id'],
                      payload=json_data['payload'])
            i.save()

db collation is currently set to uft8_general_ci.

Error returned is 'Incorrect string value'.

What is best practice to have such requests saved to the db along others?

update: By changing collation to utf8mb4_general_ci I can now write those symbols to db directly (via phpmyadmin) but still no luck with python request. Same error. Something on django side?

Vémundr
  • 395
  • 1
  • 7
  • 17

1 Answers1

0

Changing the MySQL charset to utf8mb4 will allow you to serialize the pictograms to the DB directly but in order to fix this on the Django end you will have to update your settings.py as well (see below):

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

See also MySQL “incorrect string value” error when save unicode string in Django

Victor
  • 421
  • 4
  • 7