8
I just figured out one method to avoid above errors.
Save to database
user.first_name = u'Rytis'.encode('unicode_escape')
user.last_name = u'Slatkevičius'.encode('unicode_escape')
user.save()
>>> SUCCEED
print user.last_name
>>> Slatkevi\u010dius
print user.last_name.decode('unicode_escape')
>>> Slatkevičius
Is this the only method to save strings like that into a MySQL table and decode it before rendering to templates for display?
170
None of these answers solved the problem for me. The root cause being:
You cannot store 4-byte characters in MySQL with the utf-8 character set.
MySQL has a 3 byte limit on utf-8 characters (yes, it’s wack, nicely summed up by a Django developer here)
To solve this you need to:
- Change your MySQL database, table and columns to use the utf8mb4 character set (only available from MySQL 5.5 onwards)
- Specify the charset in your Django settings file as below:
settings.py
DATABASES = {
'default': {
'ENGINE':'django.db.backends.mysql',
...
'OPTIONS': {'charset': 'utf8mb4'},
}
}
Note: When recreating your database you may run into the ‘Specified key was too long‘ issue.
The most likely cause is a CharField
which has a max_length of 255 and some kind of index on it (e.g. unique). Because utf8mb4 uses 33% more space than utf-8 you’ll need to make these fields 33% smaller.
In this case, change the max_length from 255 to 191.
Alternatively you can edit your MySQL configuration to remove this restriction but not without some django hackery
UPDATE: I just ran into this issue again and ended up switching to PostgreSQL because I was unable to reduce my VARCHAR
to 191 characters.
- [Django]-How do I get the object if it exists, or None if it does not exist in Django?
- [Django]-How to test "render to template" functions in django? (TDD)
- [Django]-Django switching, for a block of code, switch the language so translations are done in one language
126
I had the same problem and resolved it by changing the character set of the column. Even though your database has a default character set of utf-8
I think it’s possible for database columns to have a different character set in MySQL. Here’s the SQL QUERY I used:
ALTER TABLE database.table MODIFY COLUMN col VARCHAR(255)
CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL;
- [Django]-Django logging of custom management commands
- [Django]-Exclude fields in Django admin for users other than superuser
- [Django]-What is the use of PYTHONUNBUFFERED in docker file?
72
If you have this problem here’s a python script to change all the columns of your mysql database automatically.
#! /usr/bin/env python
import MySQLdb
host = "localhost"
passwd = "passwd"
user = "youruser"
dbname = "yourdbname"
db = MySQLdb.connect(host=host, user=user, passwd=passwd, db=dbname)
cursor = db.cursor()
cursor.execute("ALTER DATABASE `%s` CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci'" % dbname)
sql = "SELECT DISTINCT(table_name) FROM information_schema.columns WHERE table_schema = '%s'" % dbname
cursor.execute(sql)
results = cursor.fetchall()
for row in results:
sql = "ALTER TABLE `%s` convert to character set DEFAULT COLLATE DEFAULT" % (row[0])
cursor.execute(sql)
db.close()
- [Django]-In django, how do I sort a model on a field and then get the last item?
- [Django]-Django: How to get related objects of a queryset?
- [Django]-ForeignKey to abstract class (generic relations)
28
If it’s a new project, I’d just drop the database, and create a new one with a proper charset:
CREATE DATABASE <dbname> CHARACTER SET utf8;
- [Django]-Django – How to set default value for DecimalField in django 1.3?
- [Django]-Django render_to_string missing information
- [Django]-Django – how to unit test a post request using request.FILES
6
You can change the collation of your text field to UTF8_general_ci and the problem will be solved.
Notice, this cannot be done in Django.
- [Django]-Django.db.utils.ProgrammingError: relation "bot_trade" does not exist
- [Django]-Django Multiple Authentication Backend for one project
- [Django]-What is pip install -q -e . for in this Travis-CI build tutorial?
2
Improvement to @madprops answer – solution as a django management command:
import MySQLdb
from django.conf import settings
from django.core.management.base import BaseCommand
class Command(BaseCommand):
def handle(self, *args, **options):
host = settings.DATABASES['default']['HOST']
password = settings.DATABASES['default']['PASSWORD']
user = settings.DATABASES['default']['USER']
dbname = settings.DATABASES['default']['NAME']
db = MySQLdb.connect(host=host, user=user, passwd=password, db=dbname)
cursor = db.cursor()
cursor.execute("ALTER DATABASE `%s` CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci'" % dbname)
sql = "SELECT DISTINCT(table_name) FROM information_schema.columns WHERE table_schema = '%s'" % dbname
cursor.execute(sql)
results = cursor.fetchall()
for row in results:
print(f'Changing table "{row[0]}"...')
sql = "ALTER TABLE `%s` convert to character set DEFAULT COLLATE DEFAULT" % (row[0])
cursor.execute(sql)
db.close()
Hope this helps anybody but me
- [Django]-FileUploadParser doesn't get the file name
- [Django]-What is the use of PYTHONUNBUFFERED in docker file?
- [Django]-Django Framework – Is there a shutdown event that can be subscribed to?
1
You aren’t trying to save unicode strings, you’re trying to save bytestrings in the UTF-8 encoding. Make them actual unicode string literals:
user.last_name = u'Slatkevičius'
or (when you don’t have string literals) decode them using the utf-8 encoding:
user.last_name = lastname.decode('utf-8')
- [Django]-Dynamically adding a form to a Django formset
- [Django]-Django Admin – Disable the 'Add' action for a specific model
- [Django]-Numeric for loop in Django templates
0
Simply alter your table, no need to any thing. just run this query on database.
ALTER TABLE table_name
CONVERT TO CHARACTER SET utf8
it will definately work.
- [Django]-Passing STATIC_URL to file javascript with django
- [Django]-.filter() vs .get() for single object? (Django)
- [Django]-Django REST Framework: adding additional field to ModelSerializer