Django <-> SQL Server 2005, text encoding problem

痞子三分冷 提交于 2019-12-01 05:38:21

We use Django with SQL Server 2005. We found the same problem you did.

What ODBC driver are you using? FreeTDS?

We tried finding a good ODBC driver for linux/unix to use that would not throw the error above (and others) when unicode would come into play - and failed miserably. None of the drivers we tested - at least three, I can dig the names up if you would like - had any success in dealing with unicode strings via django-pyodbc.

What we ended up doing, sad as it might sound, was to decide to run Django on a Windows server (Apache + mod_wsgi) and use the Microsoft's SQL Native ODBC driver.

It works just fine - unicode wise - when we do that.

OK, the solution was found. In file freetds.conf there is

client charset = UTF-8

and it works exactly like it should.

in addition to the accepted response, it is possible to fix this error dirrectly in the settings.py :

DATABASES = {
    'default': {
        'ENGINE': 'sql_server.pyodbc',
        'NAME': 'MyTableName',
        'HOST': r'server.lan\server_instance_name',
        'USER': 'sa',
        'PASSWORD': 'P@SsW0Rd',
        'OPTIONS': {
            'host_is_server': True,
            "extra_params":"TDS_Version=8.0;ClientCharset=UTF-8",
            "autocommit": True,
            "driver_needs_utf8":True,
        },

     }
}

take a look at the extra_params

this don't rely on the global freetds.conf file, so it is better

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!