Alireza javanpour
Alireza javanpour

Reputation: 99

OperationalError in django when adding a new record

I have created a mysql database with Cpanel . And I have some settings for database in the settings.py :

DATABASES = {  
'default': {  
    'ENGINE': 'django.db.backends.mysql',  
    'NAME': '*****_db',  
    'USER': '******',  
    'PASSWORD': '********',  
    'HOST': 'localhost',  
    'PORT': '3306',  
    'OPTIONS': { 'init_command': 'SET storage_engine=INNODB,character_set_connection=utf8,collation_connection=utf8_unicode_ci'
    }
}  

}

but the problem is when I try to add a new record in django-admin with some arabic chars , I get this error :

OperationalError at /admin/courses/arguments/add/
(1366, "Incorrect string value: '\\xD8\\xB3\\xDA\\xAF' for column `asiatrad_db`.`courses_arguments`.`name` at row 1")

What is the problem ? Do I need to create a new database with charset on utf-8 ?

Upvotes: 0

Views: 49

Answers (1)

Ivan Grakov
Ivan Grakov

Reputation: 44

The “utf8” encoding only supports three bytes per character. The real UTF-8 encoding, which everybody uses, needs up to four bytes per character. See this article.

So use “utf8mb4” charset instead of “utf8”.

The settings.py should look as follows:

DATABASES = {  
'default': {  
    'ENGINE': 'django.db.backends.mysql',  
    'NAME': '*****_db',  
    'USER': '******',  
    'PASSWORD': '********',  
    'HOST': 'localhost',  
    'PORT': '3306',  
    'OPTIONS': { 'init_command': 'SET storage_engine=INNODB','charset': 'utf8mb4'}
}  

Upvotes: 1

Related Questions