Reputation: 343
I'm trying to connect my Django app to microsoft sql database on apache server but I get the following error messages:
django.core.exceptions.ImproperlyConfigured: 'sql_server.pyodbc' isn't an available database backend.
Try using 'django.db.backends.XXX', where XXX is one of:
'mysql', 'oracle', 'postgresql', 'sqlite3'
I have installed django-pyodbc-azure and it's showing up as part of (pip freeze list):
Django==2.1
django-pyodbc==1.1.3
django-pyodbc-azure==2.1.0.0
pyodbc==4.0.25
Here is settings.py database configuration:
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'name',
'USER': 'user',
'PASSWORD': 'password',
'HOST': 'host',
'PORT': '',
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server',
},
},
Inside site-packages folder, it's doesn't show the actual django-pyodbc-azure folder but when i run the command (pip show django-pyodbc-azure), it shows the package location (/usr/local/lib/python3.5/dist-packages) which means it's successfully installed.
So I'm not really sure what is the problem.
Upvotes: 12
Views: 32692
Reputation: 445
I had the same problem. Somehow the installation of the azure-backend messed up my project.
I removed the django-pyodbc-azure and django-mssql-backend packages since they only support the older django versions. Afterwards i installed the mssql backend from https://github.com/microsoft/mssql-django
pip uninstall django-pyodbc-azure
pip uninstall django-mssql-backend
pip install mssql-django
Then i configured the DB to use 'ENGINE': 'mssql'
After this i was able to connect to our MSSQL DB using Django 3.2 and newer Versions!
Upvotes: 13
Reputation: 11
I use python:3.9.13-slim-buster, Django 3.2 And install mssql-django
FROM python:3.9.13-slim-buster
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
ENV DEBIAN_FRONTEND noninteractive
ADD odbcinst.ini /etc/
RUN apt-get update -y && apt-get install -y curl gnupg
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
RUN curl https://packages.microsoft.com/config/debian/10/prod.list > /etc/apt/sources.list.d/mssql-release.list
RUN apt-get update -y && apt-get install -y unixodbc unixodbc-dev tdsodbc freetds-common freetds-bin freetds-dev postgresql python-scipy python-numpy python-pandas
RUN apt-get update && ACCEPT_EULA=Y apt-get -y install mssql-tools msodbcsql17
RUN echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
RUN echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
RUN apt-get update
RUN pip install mssql-django
RUN mkdir /code
COPY . /code/
COPY ./requirements.txt /code/
WORKDIR /code
RUN pip install --no-cache-dir -r requirements.txt
EXPOSE 8000
Create file odbcinst.ini
[FreeTDS]
Description=FreeTDS Driver
Driver=/usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup=/usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
requirements.txt
Django==3.2
django-environ==0.4.5
# psycopg2>=2.8.6,<2.9
uWSGI>=2.0.19.1,<2.1
Pillow >= 9.0.0,<10.0.0
settings.py
DATABASES = {
'default': {
'ENGINE': 'mssql',
'HOST': os.environ.get('DB_HOST_SITO'),
'NAME': os.environ.get('DB_NAME_SITO'),
'USER': os.environ.get('DB_USER_SITO'),
'PASSWORD': os.environ.get('DB_PASS_SITO'),
'PORT': os.environ.get('DB_PORT_SITO'),
'OPTIONS': {
'driver': 'FreeTDS',
'unicode_results': True,
'host_is_server': True,
'driver_supports_utf8': True,
'extra_params': 'tds_version=7.4',
}
},
}
Upvotes: 1
Reputation: 2451
Use mssql-django with versions of Django >=2.2 and <4.1
.
As of today current version of mssql-django==1.1.3
supports Django versions >=2.2 and <4.1
https://github.com/microsoft/mssql-django/blob/c0476cf4e49ab3dcbbab37ccb3e558216841b6dc/setup.py#L41
To use older versions of Django use django-pyodbc-azure
.
The support for Django==1.8
was added by https://github.com/microsoft/mssql-django/tree/0b0cee7030795682b37da36a48ebb065a3faa00e and removed by this: https://github.com/microsoft/mssql-django/tree/204d1fc0a4ade0ebe3e1df07a943c03b5ab5cf33.
To use Django==1.8.x
use pip install "django-pyodbc-azure<1.9"
where it Supports Microsoft SQL Server 2005, 2008/2008R2, 2012, 2014 and
Azure SQL Database: https://github.com/microsoft/mssql-django/commit/204d1fc0a4ade0ebe3e1df07a943c03b5ab5cf33#diff-7b3ed02bc73dc06b7db906cf97aa91dec2b2eb21f2d92bc5caa761df5bbc168fR241
Note:
if you are using older version of Django (<2.0.0), installing mssql-django
will force install/upgrade to newer supported version of django as part of its requirement.
One may need to go through readme.md, setup.py files to know the supported versions.
Upvotes: 2
Reputation: 29
Try to install pip install django-pyodbc-azure
https://pypi.org/project/django-pyodbc-azure/
I had the same problem and using that it worked.
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'Name_database',
'USER': 'User',
'PASSWORD': 'Password',
'HOST': 'IP',
'PORT': '1433',
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server',
},
}
}
Upvotes: 2