Hi, I see the documentation pointed out by Karen says:
"All of Django's database backends automatically convert Unicode strings into the appropriate encoding for talking to the database. They also automatically convert strings retrieved from the database into Python Unicode strings. You don't even need to tell Django what encoding your database uses: that is handled transparently." All is clear and the sky is blue now. Thank you Karen and Malcolm! On 12ζ12ζ₯, εεΎ12:13, "Karen Tracey" <[email protected]> wrote: > On Thu, Dec 11, 2008 at 10:02 PM, I.A <[email protected]> wrote: > > > Hi, > > > I'm wondering the correct way to save unicode data in MySQL through > > django's models. > > The application I'm building reads a raw email and then stores the > > text data into the database. I process the email like so: > > > import email > > mess822 = email.message_from_string(email_raw_data) > > mail_unicode_data = unicode(mess822.get_payload(), > > mess822.get_content_charset('utf-8')) > > > Nearly all of the email that I get are from Japanese clients emails, > > mess822.get_content_charset() will usually return 'iso-2022-jp' > > > Below is the model i'm using > > > class ClientsEmail(models.Model): > > mail_text = models.CharField(max_length=255) > > > And below is the mysql character settings: > > > mysql> SHOW VARIABLES LIKE 'char%'; > > > +--------------------------+----------------------------------+ > > | Variable_name | Value | > > +--------------------------+----------------------------------+ > > | character_set_client | utf8 | > > | character_set_connection | utf8 | > > | character_set_database | utf8 | > > | character_set_filesystem | binary | > > | character_set_results | utf8 | > > | character_set_server | utf8 | > > | character_set_system | utf8 | > > | character_sets_dir | /usr/local/share/mysql/charsets/ | > > +--------------------------+----------------------------------+ > > > After getting the payload data from the email, I do > > > ce = ClientsEmail(mail_text = mail_unicode_data) > > ce.save() > > > and everything looks fine. My question is, is it required/better to > > do > > ce = ClientsEmail(mail_text = mail_unicode_data.encode('utf-8')) > > instead since the db is set for utf-8? or is this part silently > > handled by django models? > > > From what I gather in this thread > > >http://groups.google.com/group/django-users/browse_thread/thread/9ba1... > > > It looks like it's required, but I would like to be sure. > > Better to rely on the current documentation: > > http://docs.djangoproject.com/en/dev/ref/unicode/ > > than a 2.5 year old conversation on the user's list (there have been many > changes in 2.5 years, including Unicode support throughout Django). > > In short, no you don't have to re-encode into a utf-8 bytestring to create > your model instance. Django accepts unicode, and the unicode will be > encoded to a bytestring using the correct encoding before it gets stored in > the DB. > > Karen --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---

