2012-07-16 57 views
3

我试图在Django和MySQL中保存unicode字符串(韩语)时出现Incorrect string value (Exception Value: Incorrect string value: '\xEA\xB0\x95\xED\x95\x98...' for column 'object_repr' at row 1)错误。我遇到的第一个问题是数据库表中每列的“错误字符串值”错误。但是,我通过更改列整理和整体数据库字符集来解决这个问题。Django和MySQL的Unicode错误

新的错误我收到有关的Unicode(个体经营)方法models.py.My models.py是因为以下几点:__unicode__功能的尝试时产生

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 

错误输出utf8字符...

有没有人知道如何解决这个错误?

+1

您是否尝试将'smart_unicode'实用程序应用于'self.full_name'(https://docs.djangoproject.com/zh/dev/ref/unicode/#conversion-functions)? – jdi 2012-07-16 05:34:02

回答

0

尝试在这样的第一本文件的添加一行:

#-*- encoding=UTF-8 -*- 
10

在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; 

对我来说帮助。

-1

我通过更改文件settings.py解决了此问题:请勿使用'ENGINE': 'django.db.backends.mysql'