Django model utf8 with legacy database

前端 未结 2 1839
星月不相逢
星月不相逢 2020-12-10 21:53

I have a legacy database with latin1 encoding. I do not have access to change it to utf8. When I read values from the model, I am getting garbled text.

I tried to u

相关标签:
2条回答
  • 2020-12-10 22:21

    If you have access to your 'settings.py' file, then you can change the settings saying that your database is using 'latin1'.

    Following is the example of the 'DATABASES' configuration in 'settings.py' file.

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

    I had similar issue earlier, checkout the link here Django database charset issue

    0 讨论(0)
  • 2020-12-10 22:41
    u = unicode(name,'latin-1')
    print u.encode('utf-8')
    
    0 讨论(0)
提交回复
热议问题