Incorrect String Value Error when Saving Unicode Strings in Django
When attempting to save unicode strings to Django's auth_user model, you may encounter a "incorrect string value" error. This error is typically caused by the MySQL character set and collation settings.
MySQL Character Set and Collation
MySQL supports the UTF-8 character set, but it has a 3-byte limit on characters. This means that it is not possible to store 4-byte unicode characters in MySQL with the standard UTF-8 character set.
Problem Explanation
When Django attempts to save unicode strings to the auth_user model, MySQL tries to convert them to the UTF-8 character set. However, if the strings contain 4-byte characters, the conversion fails and results in the "incorrect string value" error.
Solution
To resolve this issue, you can:
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', ... 'OPTIONS': {'charset': 'utf8mb4'}, } }
Additional Considerations
The above is the detailed content of How to Fix the \'Incorrect String Value\' Error When Saving Unicode Strings in Django?. For more information, please follow other related articles on the PHP Chinese website!