Emoticons from iPhone to Python / Django

I am trying to keep comments from an iPhone application, which, at the moment, may most likely include emoticons. No matter what I do, I can not save emoticons in the MySQL database ... Permanent Unicode errors.

  • Python 2.6.5
  • Django 1.2.1
  • MySQL database (installed in utf8 character set for tables and rows)
  • Saving data in the VARCHAR field (255)

I get the following message:

Incorrect string value: '\xF0\x9F\x97\xBC \xF0...' for column 'body' at row 1

The line I pass to the database is:

test_txt = u"Emoji - \U0001f5fc \U0001f60c \U0001f47b ...".encode('utf-8')

Update . Here's the model I'm using:

class ItemComment(db.Model):
  item = db.ForeignKey(Item)
  user = db.ForeignKey(Profile)
  body = db.CharField(max_length=255, blank=True, null=True)

  active = db.BooleanField(default=True)
  date_added = db.DateTimeField(auto_now_add=True)

  def __unicode__(self):
    return "%s" % (self.item)

It is strange if I try to pass this to the field that I created in MySQL and not in Django models.py, it works fine. But as soon as I register a field in Django models, it dies. Is there a way to keep them, maybe?

.
...

2. UPDATE ( U0001f5fc)

UPDATE 'table' SET 'body' = '🗼', WHERE 'table'.'id' = 1 ; args=(u'\U0001f5fc')

, :

force_unicode(smart_str(value), encoding='utf-8', strings_only=False, errors='ignore')

:

_mysql_exceptions.Warning: Incorrect string value: '\xF0\x9F\x97\xBC' for column 'body' at row 1

!!!

,

+5
2

charset utf8mb4 MySQL ( 5.5.3)

my.ini (my.cnf)

[mysqld]
character_set_server = utf8mb4
collation-server = utf8mb4_unicode_ci

SQL-

SET NAMES 'utf8mb4';

. http://dev.mysql.com/doc/refman/5.5/en/charset-connection.html

, .

Python

import re
# emoji_text is unicode
no_emoji_text = re.sub('[\xF0-\xF7][\x80-\xBF][\x80-\xBF][\x80-\xBF]', '', str(emoji_text))

.

. MySQL

+5

Django 1.11 setting.py, create sql emoji,

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'db_name',
        'USER': 'db_user',
        'PASSWORD': 'your_password',
        'OPTIONS': {'charset': 'utf8mb4'},   # note here!!!
    }
}

sql ,

CREATE DATABASE db_name CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
0

All Articles