4

I am getting an Incorrect string value (Exception Value: Incorrect string value: '\xEA\xB0\x95\xED\x95\x98...' for column 'object_repr' at row 1) error while trying to save unicode string (Korean) in Django and MySQL.First problem I had was "Incorrect string value" error for each column in the database table. However, I figured this out by changing column collation and overall database character set.

The new error I am getting is related to unicode(self) method in models.py.My models.py is as the following:

from django.db import models

# Create your models here.
class User(models.Model):
full_name = models.CharField(max_length=60)
email = models.EmailField(unique=True)
password = models.CharField(max_length=128)
birthday = models.DateField(null=True, blank=True)
gender = models.PositiveIntegerField(null=True, blank=True)
location = models.CharField(max_length=60, null=True, blank=True)
captcha = models.CharField(max_length=60, null=True, blank=True)

register_date = models.DateTimeField()
lastLogin_date = models.DateTimeField(null=True)
num_logins = models.PositiveIntegerField()

def __unicode__(self):
    return self.full_name

The error is generated when the__unicode__ function tries to output utf8 character...

Does anybody know how to fix this error?

1

3 Answers 3

10

In MySQL console execute

ALTER DATABASE django_db CHARACTER SET utf8 COLLATE utf8_unicode_ci;
ALTER TABLE django_admin_log MODIFY object_repr VARCHAR(200) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL;

For me it helps.

Sign up to request clarification or add additional context in comments.

1 Comment

for making adminsite accept unicode - confirmed to work in django 4.0.4. This is required even after changing table and db charset. Note in my case it varied slightly: ALTER TABLE django_admin_log MODIFY object_repr VARCHAR(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL;
-1

Try add one line on the first of this file like this:

#-*- encoding=UTF-8 -*-

Comments

-1

I solved this problem by changing the file settings.py: Don´t use 'ENGINE': 'django.db.backends.mysql'.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.