Django + sqlite + Unicode [英] Django + sqlite + Unicode

查看:75
本文介绍了Django + sqlite + Unicode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在通过管理站点将新记录添加到sqlite数据库时,我遇到了Unicode字符串的问题。

I faced a problem with Unicode strings while adding new records to a sqlite database through the admin site.

class Translation(BaseModel):
  .....
  translation = models.CharField(max_length=100)

当我尝试插入像'été'这样的单词时,会发生错误:

When I try to insert a word like 'été' an error occurs:

**UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 0: ordinal not in range(128)**

更新:添加了回溯: http://pastebin.com/yLYFNDGB

推荐答案

我找到了解决方案。实际上,问题不在Django或sqlite中。问题出在 unicode ()方法上。

I found a solution. Actually, the problem was not in Django or sqlite. The issue was with the unicode() method.

以前是:

def __unicode__(self):
    return "{} ({})".format(self.translation, self.word.lang.abbr)

经过明显的修复后,问题消失了:

After an obvious fix, the problem is gone:

def __unicode__(self):
    return u"{} ({})".format(self.translation, self.word.lang.abbr)

这篇关于Django + sqlite + Unicode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆