Reputation: 39909
Using SqlAlchemy, I have some String column that fails with
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 356: ordinal not in range(128)
because they are not Unicode.
I know that by doing my_string.decode('cp1252'), it works, but is it possible to define a method that will decode all the string returned from the database automatically.
With it, calling my_model.my_string, would be processed before return and decoded.
I looked into http://docs.sqlalchemy.org/en/rel_0_8/core/types.html#coercing-encoded-strings-to-unicode, which seems to be exactly what I'm looking for, but I don't know where to tell SqlAlchemy to use the CoerceUTF8
class.
Upvotes: 1
Views: 1348
Reputation: 7564
You already found the solution. What you build there is a new type. So instead of Column(String...)
or Column(Unicode...)
you do Column(CoercedString...)
.
Upvotes: 2