Django Database Encoding Problem

I have the following problem for which I did not get the correct solution after an hour of searching.

I have a MySQL database table that has a "Long Text" column. To use less storage space for the contents of files in this text column, the following compression approach was used in PHP to store the contents.

$compressed_content = bzcompress($content);
$db_compressed_content = addslashes($compressed_content);

"db_compressed_content" is stored in the database using PHP itself.

Now I can use the contents of the database using Django. I was able to come up with a model class to represent the table. "TextField" is used to represent this particular column.

Here is my exact problem, I used python 'bz2.decompress ()' to unpack and get the text content, but I get a “UnicodeEncodeError” in django when I tried to do this.

FYI, the encoding used to store content in a database using PHP, was "latin-1".

+3
source share
1 answer

: Django "utf-8" , , ( "latin1" ), . Ex:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'test_db',
        'USER': 'root',
        'PASSWORD': '',
        'HOST': '',
        'PORT': '3306',
        'OPTIONS': {
                    'charset': 'latin1',
                    'use_unicode': True, },
    }, 
}

, unicode, "use_unicode" False, , .

!!!

+3

Source: https://habr.com/ru/post/1619863/


All Articles