简体   繁体   English

将 Django 与 MSSQL 服务器连接

[英]Connecting Django with MSSQL server

I'm trying to connect my Django app to SQL Server 2016. I've tried using django-pyodbc but it doesn't support Django 1.11.我正在尝试将我的 Django 应用程序连接到 SQL Server 2016。我尝试使用 django-pyodbc,但它不支持 ZEF0F93C83E374876A61DA0D114D16。 Instead I installed django-mssql 1.8.相反,我安装了 django-mssql 1.8。 When I try to run the application I get this error.当我尝试运行应用程序时,我收到此错误。

TypeError was unhandled by user code
Message: 'NoneType' object is not callable

At execute_from_command_line(sys.argv) in manage.py.在 manage.py 中的execute_from_command_line(sys.argv)

Here is my DATABASES from settings.py:这是来自settings.py的我的数据库:

DATABASES = {
    'default': {
        'ENGINE': 'sqlserver_ado',
        'NAME': 'TEST2',
        'HOST': 'PCNAME\SQLEXPRESS',
        'USER': '',
        'PASSWORD': '',
        'OPTIONS' : {
            'provider': 'SQLOLEDB',
            'use_mars': True,
        },
    }
}

I've tried both the default and SQLOLEDB provider but always get the same error.我已经尝试了默认和 SQLOLEDB 提供程序,但总是得到相同的错误。 I've also tried with and without user and password set but the error remains the same.我也尝试过设置和不设置用户名和密码,但错误仍然相同。 I am able to connect to a local MySQL DB just fine.我能够很好地连接到本地 MySQL DB。

I'm running Windows 10, Visual Studio 2015, SQL Server Express 2016我正在运行 Windows 10、Visual Studio 2015、SQL Server Express 2016

Edit:编辑:

Here's the output from pip freeze :这是pip freeze的 output :

appdirs==1.4.3
Django==1.11
django-mssql==1.8
mysqlclient==1.3.10
packaging==16.8
pyodbc==4.0.16
pyparsing==2.2.0
pytz==2017.2
six==1.10.0

Here's my requirements.txt:这是我的requirements.txt:

django==1.11
mysqlclient==1.3.10
django-mssql==1.8

As stated in the django-mssql documentation , the latest release only supports Django 1.8, so it won't work with Django 1.11.django-mssql 文档中所述,最新版本仅支持 Django 1.8,因此它不适用于 Django 1.11。

You will have to wait until the package supports newer versions of django to upgrade.您必须等到软件包支持较新版本的 django 才能升级。 That is the problem when using Django with non-supported database backends, you depend on the third party packages maintenance, and this one seems to have trouble staying up to date with Django.这就是将 Django 与不受支持的数据库后端一起使用时的问题,您依赖于第三方软件包维护,而这个似乎无法与 Django 保持同步。

Following official django documentation (currently django 3.1) django-mssql-backend should be used.应使用django-mssql-backend遵循官方django 文档(当前为 django 3.1)。 Django-MSSQL-backend django database adapter is a fork of django-pyodbc-azure which: Django-MSSQL-backend django 数据库适配器是django-pyodbc-azure 的一个分支,它:

  • Supports Django 2.2, 3.0支持 Django 2.2、3.0
  • Supports Microsoft SQL Server 2008/2008R2, 2012, 2014, 2016, 2017, 2019支持 Microsoft SQL Server 2008/2008R2、2012、2014、2016、2017、2019
  • Compatible with Micosoft ODBC Driver for SQL Server, SQL Server Native Client, and FreeTDS ODBC drivers与适用于 SQL Server、SQL Server Native Client 和 FreeTDS ODBC 驱动程序的 Micosoft ODBC 驱动程序兼容

Other solutions django-pyodbc-azure , django-sqlserver and django-mssql as of 2020-11 look to be obsolete.截至 2020-11 年,其他解决方案django-pyodbc-azuredjango-sqlserverdjango-mssql看起来已经过时了。

You can use django-pyodbc-azure because it has support up to current versions of django 2.0.您可以使用 django-pyodbc-azure,因为它支持最新版本的 django 2.0。 After installation you need to edit in your settings file like below:安装后,您需要在设置文件中进行编辑,如下所示:

DATABASES = {
    'default': {
        'ENGINE': 'sql_server.pyodbc',
        'NAME': DB_NAME,
        'USER': USER,
        'PASSWORD': PASSWORD,
        'HOST': HOST,
        'PORT': PORT,
        'OPTIONS': {
            'driver': 'ODBC Driver 13 for SQL Server',
            'unicode_results': True,

        },
    }
}

If you install TDS library as driver then your driver will be 'driver':'Free TDS' Here 13 is the default version.如果您安装 TDS 库作为驱动程序,那么您的驱动程序将是 'driver':'Free TDS' 这里 13 是默认版本。 If your installed version is different from that then use that version number instead of 13如果您安装的版本与该版本不同,则使用该版本号而不是 13

Please check this link .请检查此链接 First you need to install mssql-django package and database configuration should be like首先你需要安装mssql-django包和数据库配置应该像

DATABASES = {
    "default": {
        "ENGINE": "mssql",
        "NAME": "Todo",
        "USER": "",
        "PASSWORD": "",
        "HOST": "127.0.0.1",
        "PORT": "1433",
        "OPTIONS": {"driver": "ODBC Driver 17 for SQL Server",
                    },
    },
}

Using mssql-django , we can connect Django to MSSQL(SQL Server) with Windows Authentication and SQL Server Authentication .使用mssql-django ,我们可以通过Windows AuthenticationSQL Server AuthenticationDjango连接到MSSQL(SQL Server) *I use SQL Server 2019 Express . *我使用SQL 服务器 2019 Express

With Windows Authentication , to connect Django to MSSQL using mssql-django , set the code below to "settings.py" .使用Windows 身份验证,要使用mssql-djangoDjango连接到MSSQL ,请将下面的代码设置为“settings.py” This code below is the example of Django and MSSQL in the same Windows Computer(localhost) and "ENGINE" must be "mssql" and "NAME" is for Database name "test" and "DESKTOP-QVRCPTA" for "HOST" is Windows Computer Name(Device name) .下面的代码是DjangoMSSQL同一台 Windows 计算机(本地主机)中的示例, “ENGINE”必须为“mssql”“NAME”用于数据库名称“test”“DESKTOP-QVRCPTA”用于“HOST”ZAEA234EBZ8CE0AACD93006计算机名称(设备名称) *Keep it blank for "PORT" because there will be error if setting any port number eg "2244" , "9877" or even "1433" which is the default port number of MSSQL : *将“PORT”留空,因为如果设置任何端口号,例如“2244”“9877”甚至“1433” ,这是MSSQL的默认端口号,都会出错:

# "settings.py"

DATABASES = {
    'default':{
        'ENGINE':'mssql',                    # Must be "mssql"
        'NAME':'test',                       # DB name "test"
        'HOST':'DESKTOP-QVRCPTA\SQLEXPRESS', # <server>\<instance>
        'PORT':'',                           # Keep it blank
        'OPTIONS': {
            'driver': 'ODBC Driver 17 for SQL Server',
        },
    }
}

In addition, "DESKTOP-QVRCPTA" can be replaced with "localhost" and "USER" and "PASSWORD" can be put with empty string and "PORT" can be removed as shown below to connect Django to MSSQL with Windows Authentication :此外, “DESKTOP-QVRCPTA”可以替换为“localhost”“USER”“PASSWORD”可以放入空字符串“PORT”可以删除,如下图所示,通过Windows 身份验证Django连接到MSSQL

# "settings.py"

DATABASES = {
    'default':{
        'ENGINE':'mssql',
        'NAME':'test',
        'USER':'',                     # Keep it blank
        'PASSWORD':'',                 # Keep it blank
        'HOST':'localhost\SQLEXPRESS', # "localhost" is also possible
        # 'PORT':'',                   # Can be removed
        'OPTIONS': {
            'driver': 'ODBC Driver 17 for SQL Server',
        },
    }
}

With SQL Server Authentication , to connect Django to MSSQL using mssql-django , set the code for Windows Authentication as shown above with "USER" and "PASSWORD" to "settings.py" .使用SQL 服务器身份验证,使用mssql-djangoDjango连接到MSSQL ,设置Windows 身份验证的代码,如上所示,使用“USER”“PASSWORD ”设置 *The difference between the code for Windows Authentication and SQL Server Authentication is only "USER" and "PASSWORD" : * Windows AuthenticationSQL Server Authentication的代码只有“USER”“PASSWORD”的区别:

# "settings.py"

DATABASES = {
    'default':{
        'ENGINE':'mssql',  
        'NAME':'test',           
        'USER':'john',                       # Username "john"
        'PASSWORD':'johnpw',                 # Password "johnpw"
        'HOST':'DESKTOP-QVRCPTA\SQLEXPRESS',
        'PORT':'',                           
        'OPTIONS': {
            'driver': 'ODBC Driver 17 for SQL Server',
        },
    }
}

In addition, same as the code for Windows Authentication , "DESKTOP-QVRCPTA" can be replaced with "localhost" and "PORT" can be removed as shown below to connect Django to MSSQL with SQL Server Authentication :此外,与Windows Authentication的代码相同, “DESKTOP-QVRCPTA”可以替换为“localhost” ,并且可以删除“PORT” ,如下所示,以将Django连接到MSSQLZ9778840A0100CB30C5B82Z7 Server Authentication Authentication

# "settings.py"

DATABASES = {
    'default':{
        'ENGINE':'mssql',
        'NAME':'test',
        'USER':'john',        
        'PASSWORD':'johnpw',
        'HOST':'localhost\SQLEXPRESS', # "localhost" is also possible
        # 'PORT':'',                   # Can be removed   
        'OPTIONS': {
            'driver': 'ODBC Driver 17 for SQL Server',
        },
    }
}

Next, install the latest package mssql-django :接下来,安装最新的 package mssql-django

pip install mssql-django

Then, make migrations and migrate:然后,进行迁移并迁移:

python manage.py makemigrations && python manage.py migrate

Then, create superuser:然后,创建超级用户:

python manage.py createsuperuser

Now, we could connect Django to MSSQL with Windows Authentication and SQL Server Authentication , then create the tables for "test" database .现在,我们可以使用Windows AuthenticationSQL Server AuthenticationDjango连接到MSSQL ,然后为“test”数据库创建表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM