Menu Close

How to fix MySQL “incorrect string value” error when save unicode string in Python Django?

Sometimes, we want to fix MySQL "incorrect string value" error when save unicode string in Python Django.

In this article, we’ll look at how to fix MySQL "incorrect string value" error when save unicode string in Python Django.

How to fix MySQL "incorrect string value" error when save unicode string in Python Django?

To fix MySQL "incorrect string value" error when save unicode string in Python Django, we can change some options in the Django app settings.

To fix this, we write

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

in settings.py.

We can use utf8mb4 characters with MySQL 5.5 or later.

Conclusion

To fix MySQL "incorrect string value" error when save unicode string in Python Django, we can change some options in the Django app settings.

Posted in Python, Python Answers