views:

24

answers:

1

When saving an object in the Django admin I get this error, UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 63: ordinal not in range(128)

my database is unicode (postgres) The object being saved has some french characters. I have never had issues before saving objects when I was using MySQL.

A: 

Without seeing any code it's tricky to guess what's going on, but I suspect you're passing a unicode to something which is then converting that to a str.

E.g.

>>> str(u'\xe9')

Traceback (most recent call last):
  File "<pyshell#39>", line 1, in <module>
    str(u'\xe9')
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 0: ordinal not in range(128)

You should probably be encoding whatever you're passing in so it turns into a suitable byte stream. If it's expecting unicode in a str then utf8 is a likely choice, but consult the docs for whatever modules you're using.

yourstring = yourunicode.encode("utf8")

unicode isn't a byte encoding so it can't be used to interface two system ... for that an encoding (e.g. UTF8, UTF16, etc) needs to be applied which both sides understand. So having postgres in unicode and python in unicode doesn't allow you to just pass around whatever their internal representations are - at the interface you'll need to specify which encoding everyone should use.

What every coder should know about unicode

pycruft