Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django model.charfield - unicode or not unicode

I transfered my project to another computer and get an error while running a view.

I'm getting some informations of a model and want to save them to XML by using XMLGenerator.

On the one computer it works fine, type() of the model.charField() returns "unicode"

On the new computer it did not work, type() of the model.charField() returns "str"

The working computer has Python 2.7.2

The not working computer has Python 2.5.2

So on the not working computer I did not get unicode which can be handled by XMLGenerator. I tried to work around the problem by running .decode("utf-8") on the string which is served by the model and it worked.

But how can I know what encoding the string is? I guessed now that it has the same encoding as in the database but am I right?

regards Martin

like image 905
Martin Avatar asked Jan 30 '13 14:01

Martin


People also ask

Is Python unicode or Ascii?

Python's string type uses the Unicode Standard for representing characters, which lets Python programs work with all these different possible characters.

What is the use of unicode in Django?

If you define a __unicode__() method, Django will call it when it needs to render an object in a context where a string representation is needed (e.g. in the model's admin pages). The documentation says: The __unicode__() method is called whenever you call unicode() on an object.

Is unicode the same as string?

Unicode is a standard encoding system that is used to represent characters from almost all languages. Every Unicode character is encoded using a unique integer code point between 0 and 0x10FFFF . A Unicode string is a sequence of zero or more code points.

Does Django automatically encode user input?

All of Django's database backends automatically convert strings into the appropriate encoding for talking to the database. They also automatically convert strings retrieved from the database into strings. You don't even need to tell Django what encoding your database uses: that is handled transparently.


1 Answers

could you please check the mysql collation settings? if those are also the same? from django doc: "In many cases, this default will not be a problem. However, if you really want case-sensitive comparisons on a particular column or table, you would change the column or table to use the utf8_bin collation. The main thing to be aware of in this case is that if you are using MySQLdb 1.2.2, the database backend in Django will then return bytestrings (instead of unicode strings) for any character fields it receive from the database." see django doc collation settings

like image 67
tiborka Avatar answered Sep 22 '22 15:09

tiborka