Django model.charfield - unicode or not unicode

I transferred the project to another computer and I get an error when starting the view.

I am getting some information about a model and want to store it in XML using XMLGenerator.

On one computer it works fine, type()

model.charField()

returns "unicode"

On new computer it didn't work, type()

model.charField()

returns "str"

The work computer has Python 2.7.2

A non-working computer has Python 2.5.2

So, on a non-working computer, I didn't get a unicode that XMLGenerator can handle. I tried to solve the problem by running .decode ("utf-8") on the line served by the model and it worked.

But how can I find out what the encoding of the string is? I already guessed that it has the same encoding as in the database, but am I right?

thinks Martin

+3


source to share


2 answers


Could you check your mysql collation settings? if they are the same? from django doc: "In many cases this default will not be a problem. However, if you really want to compare case to case in a specific column or table, you must change the column or table to use utf8_bin collation. The main thing to know about in In this case, if you are using MySQLdb 1.2.2, the database backend in Django then returns bytes (instead of unicode strings) for any character fields it receives from the database. " see django doc mapping settings



+5


source


Let's say we have this:

a = unicode('a')
b = str('b')

      

Quick check:

print type(a)
print type(b)

      



If you want to test them, you can do:

if isinstance(a, str):

if isinstance(a, unicode):

      

It looks like it would come up with content:

c = str(a)
d = unicode(b)

      

0


source







All Articles