BartoNaz
BartoNaz

Reputation: 2893

Unicode strings in Django + SQLite

I have a model in Django 1.6:

class Text(models.Model):
  text = models.CharField()
  def __unicode__(self):
    return self.text

I have a cyrillic text stored in the table in this field.

When I try to request the object from table in the Django shell I get confusing result:

print t

Returns the correct string stored in the table

print t.text

Returns u'u\0427u\0438'

Why doesn't it return the field value properly?

Upvotes: 0

Views: 900

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121266

When you use print, Python will try to encode Unicode values to the terminal or console codec using sys.stdout.encoding; if this is set to an encoding that is not capable of encoding your data then a UnicodeEncodingException is thrown.

You tried used repr() on the other hand, or simply let t.text echo in an interactive session, which comes down to the same thing. repr() will give you a ASCII-safe representation of a string literal, a value that can be pasted right back into the interpreter to recreate the string. To keep this ASCII-safe, all codepoints that are not printable ASCII characters are represented using escape sequences instead; newlines become \n again, and codepoints passed the Latin-1 range use \uxxxx representations.

When you use print model on the other hand, the default .__str__() method on models is used; this method encodes the Unicode value returned by your .__unicode__() method to UTF-8.

If you are seeing correct output when using print t, an exception for print t.text and print repr(t.text) works, your terminal is incorrectly configured. You can explicitly encode values in that case, to UTF-8 since that already worked for you:

print t.text.encode('utf8')

Upvotes: 2

Related Questions