Windows10
SQL Server 2019
py -V ==== Python 3.9.2
pip -V ==== pip 21.0.1
python -m django –version ==== 3.0
pip list
—————————–
Package Version
——————– ——-
asgiref 3.3.1
Django 3.0
django-mssql-backend 2.8.1
pip 21.0.1
pyodbc 4.0.30
pytz 2021.1
setuptools 54.1.1
sqlparse 0.4.1
# import os,sys
# import pyodbc
# for drive in pyodbc.drivers():
# print(drive)
#
# SQL Server
# Oracle in XE
# MySQL ODBC 8.0 ANSI Driver
# MySQL ODBC 8.0 Unicode Driver
# SQL Server Native Client 11.0
# SQL Server Native Client RDA 11.0
# ODBC Driver 17 for SQL Server
DATABASES = {
‘default’: {
‘ENGINE’: ‘sql_server.pyodbc’,
‘HOST’: ‘DESKTOP-U31HLSASQLEXPRESS’,
‘PORT’: ”,
‘NAME’: ‘DJANGO_MSSQL’,
‘USER’: ‘sa’,
‘PASSWORD’: ‘1234567’,
‘OPTIONS’: {
‘driver’: ‘ODBC Driver 17 for SQL Server’,
},
},
}|
works, thank you
Thanks a lot
what if its a trusted connection and uses windows authentication?
Thank you so much
showing error AttributeError: 'Query' object has no attribute 'explain_query'
Thanks
I am connecting through windows authentication.
Hello there,
I`m using Microsoft SQL server Studio 19 and
other version of django, pip and python are;
asgiref 3.5.2
Django 4.0.6
mssql-django 1.1.3
pip 22.1.2
pyodbc 4.0.34
pytz 2022.1
setuptools 62.6.0
sqlparse 0.4.2
tzdata 2022.1
wheel 0.37.1
the connection string i`m using is given below;
DATABASES = {
'default': {
'ENGINE': 'mssql',
'NAME': 'CDSL',
'USER': 'ASHFAQ-DPO',
'PASSWORD': '218312',
'HOST': 'myserver.database.windows.net',
'PORT': '',
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server',
},
},
}
I`m unable to connect SQL with django.
Even when i run python manage.py shell
then import os, sys
import pyodbc
for driver in pyodbc.drivers():
print(driver)
nothing shows, please help to resolve the issue.
when i try to run server following error occur
django.core.exceptions.ImproperlyConfigured: 'sql_server.pyodbc' isn't an available database backend or couldn't be imported. Check the above exception. To use one of the built-in backends, use 'django.db.backends.XXX', where XXX is one of:
'mysql', 'oracle', 'postgresql', 'sqlite3'
Super bro…I m facing same issue
Thank you so much brother.
Keep up the good work!
Tried for pyodbc, there is error while migration.
pip install mssql-django will be the solution.
django.db.utils.InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
Bro I'm facing this error
I installed odbc driver manager also and connected also
Can you resolve this?
can solve this problem:
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial…Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:Usersmepatanaconda3libsite-packagesdjangocoremanagement__init__.py", line 419, in execute_from_command_line
utility.execute()
File "C:Usersmepatanaconda3libsite-packagesdjangocoremanagement__init__.py", line 413, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:Usersmepatanaconda3libsite-packagesdjangocoremanagementbase.py", line 354, in run_from_argv
self.execute(*args, **cmd_options)
File "C:Usersmepatanaconda3libsite-packagesdjangocoremanagementbase.py", line 398, in execute
output = self.handle(*args, **options)
File "C:Usersmepatanaconda3libsite-packagesdjangocoremanagementbase.py", line 89, in wrapped
res = handle_func(*args, **kwargs)
File "C:Usersmepatanaconda3libsite-packagesdjangocoremanagementcommandsmigrate.py", line 244, in handle
post_migrate_state = executor.migrate(
File "C:Usersmepatanaconda3libsite-packagesdjangodbmigrationsexecutor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "C:Usersmepatanaconda3libsite-packagesdjangodbmigrationsexecutor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "C:Usersmepatanaconda3libsite-packagesdjangodbmigrationsexecutor.py", line 227, in apply_migration
state = migration.apply(state, schema_editor)
File "C:Usersmepatanaconda3libsite-packagesdjangodbmigrationsmigration.py", line 126, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "C:Usersmepatanaconda3libsite-packagesdjangodbmigrationsoperationsmodels.py", line 528, in database_forwards
alter_together(
File "C:Usersmepatanaconda3libsite-packagessql_serverpyodbcschema.py", line 156, in alter_unique_together
self.execute(sql)
File "C:Usersmepatanaconda3libsite-packagessql_serverpyodbcschema.py", line 861, in execute
sql = str(sql)
File "C:Usersmepatanaconda3libsite-packagesdjangodbbackendsddl_references.py", line 201, in _str_
return self.template % self.parts
KeyError: 'include'
Thanks a lot really useful
Thank you very much!
Hi I did exactly as suggested in the video however I get the following error
Please help if you can
(mysite) C:Python_Vikmysitemyfirstsite>python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, polls, sessions
Running migrations:
Applying polls.0004_choice_question…Traceback (most recent call last):
File "C:Python_Vikmysitelibsite-packagesdjangodbbackendsutils.py", line 82, in _execute
return self.cursor.execute(sql)
File "C:Python_Vikmysitelibsite-packagessql_serverpyodbcbase.py", line 553, in execute
return self.cursor.execute(sql, params)
pyodbc.ProgrammingError: ('42S01', "[42S01] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]There is already an object named 'polls_question' in the database. (2714) (SQLExecDirectW)")
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:Python_Vikmysitemyfirstsitemanage.py", line 22, in <module>
main()
File "C:Python_Vikmysitemyfirstsitemanage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:Python_Vikmysitelibsite-packagesdjangocoremanagement__init__.py", line 419, in execute_from_command_line
utility.execute()
File "C:Python_Vikmysitelibsite-packagesdjangocoremanagement__init__.py", line 413, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:Python_Vikmysitelibsite-packagesdjangocoremanagementbase.py", line 354, in run_from_argv
self.execute(*args, **cmd_options)
File "C:Python_Vikmysitelibsite-packagesdjangocoremanagementbase.py", line 398, in execute
output = self.handle(*args, **options)
File "C:Python_Vikmysitelibsite-packagesdjangocoremanagementbase.py", line 89, in wrapped
res = handle_func(*args, **kwargs)
File "C:Python_Vikmysitelibsite-packagesdjangocoremanagementcommandsmigrate.py", line 244, in handle
post_migrate_state = executor.migrate(
File "C:Python_Vikmysitelibsite-packagesdjangodbmigrationsexecutor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "C:Python_Vikmysitelibsite-packagesdjangodbmigrationsexecutor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "C:Python_Vikmysitelibsite-packagesdjangodbmigrationsexecutor.py", line 227, in apply_migration
state = migration.apply(state, schema_editor)
File "C:Python_Vikmysitelibsite-packagesdjangodbmigrationsmigration.py", line 126, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "C:Python_Vikmysitelibsite-packagesdjangodbmigrationsoperationsmodels.py", line 92, in database_forwards
schema_editor.create_model(model)
File "C:Python_Vikmysitelibsite-packagessql_serverpyodbcschema.py", line 809, in create_model
self.execute(sql, params or None)
File "C:Python_Vikmysitelibsite-packagessql_serverpyodbcschema.py", line 872, in execute
cursor.execute(sql, params)
File "C:Python_Vikmysitelibsite-packagesdjangodbbackendsutils.py", line 98, in execute
return super().execute(sql, params)
File "C:Python_Vikmysitelibsite-packagesdjangodbbackendsutils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:Python_Vikmysitelibsite-packagesdjangodbbackendsutils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:Python_Vikmysitelibsite-packagesdjangodbbackendsutils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:Python_Vikmysitelibsite-packagesdjangodbutils.py", line 90, in _exit_
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:Python_Vikmysitelibsite-packagesdjangodbbackendsutils.py", line 82, in _execute
return self.cursor.execute(sql)
File "C:Python_Vikmysitelibsite-packagessql_serverpyodbcbase.py", line 553, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: ('42S01', "[42S01] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]There is already an object named 'polls_question' in the database. (2714) (SQLExecDirectW)")
Thank you so much. You saved me a lot of time.
Thanks bro
i mistakenly migrated before coonecting the database, what to do? i cant see tables in mine
THX)))
¡Perfect! Worked for me. ✌
Thanks buddy
Thank you so much. Our love hindi .
Very useful thank you…. Could you tell us how to access the db in the django app?