How to configure the Database setting Django-MSSQL using django-pyodbc (ubuntu 16.04)?

孤人 提交于 2019-12-06 12:45:40

I assume you're hosting on Windows, since you're attempting to connect to localhost. I'd highly recommend using the django-pyodbc-azure engine (https://github.com/michiya/django-pyodbc-azure), as it can be used for both local SQL Server or on Azure, and is the best maintained Django SQL Server package I've seen over several years of use. To install:

pip install django-pyodbc-azure

This will also install the pyodbc dependency. Since you're running Django on Windows (otherwise, you wouldn't be connecting to localhost), you can use the native client. Here's what I'd start with for your settings:

DATABASES = {
    'default': {
        'ENGINE': 'sql_server.pyodbc',
        'NAME': 'videogame',
        'USER': 'kucing',
        'PASSWORD': 'xxxxx',
        'HOST': 'localhost',
        'PORT': '8888',

        'OPTIONS': {
            'driver': 'SQL Server Native Client 13.0',
        },
    },
}

Also, you should never post your password on Stack Overflow! I would highly recommend changing it. Typically, if you post your password as xxxxx or something like that, people will understand why. Good luck!

Saber Alex

I've manage to figure out the issue. Because, I'm running both the Django application and the MS SQL server in linux, I need to change my driver to FreeTDS. This link is useful: How to install freetds in Linux?

After I finish installing the FreeTDS driver on my host (Ubuntu), I updated the Databases setting as follow:

DATABASES = {
    'default': {
        'ENGINE': 'sql_server.pyodbc',
        'NAME': 'videogame',
        'USER': 'sa',
        'PASSWORD': 'xxxxx',
        'HOST': 'localhost',
        'PORT': '8888',
        'OPTIONS' : {
            'driver': 'FreeTDS',
            'unicode_results': True,
            'host_is_server': True,
            'extra_params': 'tds_version=7.0;',
            }
    }
}

Then I create a superuser using this command:

python manage.py createsuperuser

And Lastly, I do the Database migration:

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