Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django + 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)

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)**

Update: Added traceback: http://pastebin.com/yLYFNDGB

like image 273
Alex G.P. Avatar asked Mar 30 '26 19:03

Alex G.P.


1 Answers

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

Previously it was:

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)
like image 114
Alex G.P. Avatar answered Apr 02 '26 05:04

Alex G.P.