我得到一个 不正确的字符串值(异常值:不正确的字符串值:'\xEA\xB0\x95\xED\x95\x98...' 对于第 1 行的列 'object_repr')
尝试在 Django 和 MySQL 中保存 unicode 字符串(韩语)时出错。我遇到的第一个问题是数据库表中每一列的“字符串值不正确”错误。但是,我通过更改列排序规则和整个数据库字符集来解决这个问题。
我得到的新错误与 models.py 中的 unicode(self) 方法有关。我的 models.py 如下:
from django.db import models
# Create your models here.
class User(models.Model):
full_name = models.CharField(max_length=60)
email = models.EmailField(unique=True)
password = models.CharField(max_length=128)
birthday = models.DateField(null=True, blank=True)
gender = models.PositiveIntegerField(null=True, blank=True)
location = models.CharField(max_length=60, null=True, blank=True)
captcha = models.CharField(max_length=60, null=True, blank=True)
register_date = models.DateTimeField()
lastLogin_date = models.DateTimeField(null=True)
num_logins = models.PositiveIntegerField()
def __unicode__(self):
return self.full_name
当 __unicode__
函数试图输出 utf8 字符时产生错误...
有人知道如何解决这个错误吗?
最佳答案
在MySQL控制台执行
ALTER DATABASE django_db CHARACTER SET utf8 COLLATE utf8_unicode_ci;
ALTER TABLE django_admin_log MODIFY object_repr VARCHAR(200) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL;
对我来说这很有帮助。
关于mysql - Django 和 MySQL unicode 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11497744/