0

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.

asked Aug 27, 2010 at 14:01
1
  • 1
    Knowing which adapter you're using as well as seeing a bit of sample code that causes the problem will help. Commented Aug 27, 2010 at 14:08

1 Answer 1

2

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

answered Aug 27, 2010 at 15:29
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.