django字符集与MySQL怪异

Pau*_*jan 21 mysql django django-models mysql-error-1267

我正在看

OperationalError (1267, "Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation '='")
Run Code Online (Sandbox Code Playgroud)

看起来我的一些变量是UTF8字符串

'name':'p\xc7\x9d\xca\x87\xc9\x9f\xc4\xb1\xc9\xa5s徽章'

这是配置问题吗?如果是这样,我该如何解决?我想处理Unicode中的所有内容(我认为).

web*_*kie 43

您可以通过shell更改表编码:

$ manage.py shell
>>> from django.db import connection
>>> cursor = connection.cursor()
>>> cursor.execute('SHOW TABLES')
>>> results=[]
>>> for row in cursor.fetchall(): results.append(row)
>>> for row in results: cursor.execute('ALTER TABLE %s CONVERT TO CHARACTER SET utf8 COLLATE     utf8_general_ci;' % (row[0]))
Run Code Online (Sandbox Code Playgroud)

https://mayan.readthedocs.org/en/v0.13/faq/index.html

  • 这很棒!但请记住,您的数据库字符集将保持不变,更改它也执行:`ALTER DATABASE databasename CHARACTER SET utf8;` (6认同)

Tec*_*ard 11

您的数据库似乎默认为latin1_swedish_ci,因此无法接受所有utf8字符.您需要更改MySQL数据库表的配置以使用utf8_general_ci.可以在MySQL Performance Blog上找到关于此的一篇好博文(包含指向工具的链接)