1

I have a project working with sqlite3 (default database) and I am trying to use Microsoft SQL Server instead.

I was following these steps and I have already installed the pymssql package, but right now I don't know what I need to do to connect my Django to my SQL Server database.

3
  • you need to add DATABASE settings( credentials) to your settings.py Commented Feb 22, 2019 at 13:06
  • 1
    In addition to @ans2human, you could also follow this doc: django-mssql.readthedocs.io/en/latest/… Commented Feb 22, 2019 at 13:11
  • You can use MSSQL database backend. find more documentation from here Commented Feb 22, 2019 at 13:31

2 Answers 2

1

One way is to use django-mssql package:

  1. Install the package via PIP : pip install django-mssql

  2. Add relevant settings of your DB to settings.py.

    DATABASES = {
    'default': {
        'NAME': 'my_database',
        'ENGINE': 'sqlserver_ado',   # <-- don't change this as this is module name 
        'HOST': 'dbserver\\ss2012',
        'USER': '',
        'PASSWORD': '',
     }
    }
    

Do not include a django.db.backends. prefix. That is only for core backends that are included with Django, not 3rd party backends.

Sign up to request clarification or add additional context in comments.

1 Comment

already try that and is always giving me this error django.core.exceptions.ImproperlyConfigured: 'sqlserver_ado' isn't an available database backend.
1

In order the ORM to work as expected you have to use a database backend. You can't just set the driver to pymssql because the ORM doesn't know how to use it. Keep in mind that at this point there is no official myssql database backend released by Django. django-mssql might do the job but the last supported Django version is 1.8.

I suggest you to try https://pypi.org/project/django-pyodbc-azure/. It looks like it supports Django 2.1. But you will have to use pyodbc instead of pymssql.

2 Comments

already try that , and is allways giving me this error ` The data source name was not found and no default driver(SQLDriverConnect) was specified)`
well i already found the solution and put DRIVER={SQL Server} but now i have another error that i cant import the models to the sql

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.