2012-07-16 55 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'