Benjamin
Benjamin

Reputation: 1253

Django unable to save unicode string in MySQL (OperationalError - 1366, "Incorrect string value")

I've got a TextField in one of my models. I tried inserting a string of Japanse characters into the database and I got this error:

OperationalError at /admin/pages/page/add/
(1366, "Incorrect string value: '\\xE3\\x83\\x91\\xE3\\x83\\xAF...' for column 'body' at row 1")

I thought that Django, Python, and MySQL supported Unicode and uses it first. What is going on and how can I fix it?

Upvotes: 0

Views: 2634

Answers (3)

Alok
Alok

Reputation: 10554

In settings.py add options in database config

'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME':  '',
    'USER': '',
    'PASSWORD': '',
    'OPTIONS': {'charset': 'utf8mb4'}, // this line should help
}

Upvotes: 0

Ratnesh Varma
Ratnesh Varma

Reputation: 69

you only need to do flowing while saving the value.

 class Employee(models.Model):
        name = models.CharField(max_length=100)

        def save(self, *args, **kwargs):
           super(Employee, self).save(*args, **kwargs)
           self.name = str(self.name.encode('unicode_escape'))

you need to encode with unicode_escape before save as mention in above example (self.name = str(self.name.encode('unicode_escape'))) then you will be able to save any value (including Japanese chars) and no need to do any other setting or db changes

Upvotes: 1

Moinuddin Quadri
Moinuddin Quadri

Reputation: 48077

It is not the Python/Django related issue. Your MySQL table column doesn't supports the unicode format you are currently using.

Default character set used by MySQL is utf-8. If you want to change character set for any particular column, you may run the query as:

ALTER TABLE db.table MODIFY COLUMN my_column VARCHAR(255)
CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL;

where:

  • db: your database
  • table: name of the table
  • my_column: name of column you want to modify

Upvotes: 3

Related Questions