django python collation error

两盒软妹~` 提交于 2021-02-18 12:08:47

问题


What is the reason for the following error? when i try to filter with:

if MyObject.objects.filter(location = aDictionary['address']):

where location is defined as:

location = models.CharField(max_length=100, blank=True, default='')

I get the following error when aDictionary['address'] contains a string with a non-alphanumeric character (for example Kīhei):

  File "/usr/lib/pymodules/python2.6/MySQLdb/connections.py", line 35, in defaul
terrorhandler
    raise errorclass, errorvalue
_mysql_exceptions.OperationalError: (1267, "Illegal mix of collations (latin1_sw
edish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation '='")

回答1:


Alter the database in MySQL like so:

ALTER TABLE foo CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;

When creating a new database, remember to create with the right collate settings:

CREATE DATABASE foo CHARACTER SET utf8 COLLATE utf8_general_ci;

More discussion here.




回答2:


Python is using Unicode strings, and your database is not. Change your database collation to use utf8 and you should be fine.



来源:https://stackoverflow.com/questions/7083421/django-python-collation-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!