I'm trying to connect to an external SQL Server Express database using the django-pyodbc version at: this github link but I keep getting this error when I run manage.py shell to check if I can execute come ORM commands
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 196, in run_from_argv
self.execute(*args, **options.__dict__)
File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py",line 232, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py",line 371, in handle
return self.handle_noargs(**options)
File "/usr/local/lib/python2.7/site-packages/django/core/management/commands/shell.py", line 45, in handle_noargs
from django.db.models.loading import get_models
File "/usr/local/lib/python2.7/site-packages/django/db/__init__.py", line 40, in <module>
backend = load_backend(connection.settings_dict['ENGINE'])
File "/usr/local/lib/python2.7/site-packages/django/db/__init__.py", line 34, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/usr/local/lib/python2.7/site-packages/django/db/utils.py", line 93, in __getitem__
conn = backend.DatabaseWrapper(db, alias)
File "/usr/local/lib/python2.7/site-packages/sql_server/pyodbc/base.py", line 133, in __init__
self.ops = DatabaseOperations(self)
File "/usr/local/lib/python2.7/site-packages/sql_server/pyodbc/operations.py", line 10, in __init__
super(DatabaseOperations, self).__init__()
TypeError: __init__() takes exactly 2 arguments (1 given)
My settings.py has this:
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'DBName',
'HOST': '192.168.29.1',
'USER': 'user',
'PASSWORD': 'pswd',
'PORT': 1433,
}
}
DATABASE_OPTIONS = {
'driver': '/usr/local/lib/libtdsodbc.so',
'dsn': 'FreeTDS',
'extra_params': "TDS_VERSION=8.0"
}
I'm completely at a loss as to what I'm supposed to do, so all ideas are welcome. Essentially what I want is to be able to use a small app I've made to interface with the SQL Server database, while being able to use normal DJango ORM commands inplace of pyodbc connection strings (and BTW using pyodbc, I can connect to the SQL Server database without an issue)
it seem to me that you are using the old repo for django-pyodbc at http://code.google.com/p/django-pyodbc/
make sure to cleanly uninstall the old version and then reinstall
pip uninstall sql-server.pyodbc
pip install git+https://github.com/avidal/django-pyodbc.git
Related
My mssql database is inside the docker container. When trying to connect to the database using azure data studio I don't get any errors, whereas when I'm trying to connect to the db using Django I get the following error:
Traceback (most recent call last):
File "/usr/local/Cellar/python#3.9/3.9.5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/threading.py", line 954, in _bootstrap_inner
self.run()
File "/usr/local/Cellar/python#3.9/3.9.5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/threading.py", line 892, in run
self._target(*self._args, **self._kwargs)
File "/Users/aleksandr/Desktop/teplo/teplo_back/venv/lib/python3.9/site-packages/django/utils/autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "/Users/aleksandr/Desktop/teplo/teplo_back/venv/lib/python3.9/site-packages/django/core/management/commands/runserver.py", line 121, in inner_run
self.check_migrations()
File "/Users/aleksandr/Desktop/teplo/teplo_back/venv/lib/python3.9/site-packages/django/core/management/base.py", line 486, in check_migrations
executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
File "/Users/aleksandr/Desktop/teplo/teplo_back/venv/lib/python3.9/site-packages/django/db/migrations/executor.py", line 18, in __init__
self.loader = MigrationLoader(self.connection)
File "/Users/aleksandr/Desktop/teplo/teplo_back/venv/lib/python3.9/site-packages/django/db/migrations/loader.py", line 53, in __init__
self.build_graph()
File "/Users/aleksandr/Desktop/teplo/teplo_back/venv/lib/python3.9/site-packages/django/db/migrations/loader.py", line 220, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "/Users/aleksandr/Desktop/teplo/teplo_back/venv/lib/python3.9/site-packages/django/db/migrations/recorder.py", line 77, in applied_migrations
if self.has_table():
File "/Users/aleksandr/Desktop/teplo/teplo_back/venv/lib/python3.9/site-packages/django/db/migrations/recorder.py", line 55, in has_table
with self.connection.cursor() as cursor:
File "/Users/aleksandr/Desktop/teplo/teplo_back/venv/lib/python3.9/site-packages/django/utils/asyncio.py", line 33, in inner
return func(*args, **kwargs)
File "/Users/aleksandr/Desktop/teplo/teplo_back/venv/lib/python3.9/site-packages/django/db/backends/base/base.py", line 259, in cursor
return self._cursor()
File "/Users/aleksandr/Desktop/teplo/teplo_back/venv/lib/python3.9/site-packages/mssql/base.py", line 230, in _cursor
conn = super()._cursor()
File "/Users/aleksandr/Desktop/teplo/teplo_back/venv/lib/python3.9/site-packages/django/db/backends/base/base.py", line 235, in _cursor
self.ensure_connection()
File "/Users/aleksandr/Desktop/teplo/teplo_back/venv/lib/python3.9/site-packages/django/utils/asyncio.py", line 33, in inner
return func(*args, **kwargs)
File "/Users/aleksandr/Desktop/teplo/teplo_back/venv/lib/python3.9/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
self.connect()
File "/Users/aleksandr/Desktop/teplo/teplo_back/venv/lib/python3.9/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/Users/aleksandr/Desktop/teplo/teplo_back/venv/lib/python3.9/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
self.connect()
File "/Users/aleksandr/Desktop/teplo/teplo_back/venv/lib/python3.9/site-packages/django/utils/asyncio.py", line 33, in inner
return func(*args, **kwargs)
File "/Users/aleksandr/Desktop/teplo/teplo_back/venv/lib/python3.9/site-packages/django/db/backends/base/base.py", line 200, in connect
self.connection = self.get_new_connection(conn_params)
File "/Users/aleksandr/Desktop/teplo/teplo_back/venv/lib/python3.9/site-packages/mssql/base.py", line 326, in get_new_connection
conn = Database.connect(connstr,
django.db.utils.OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]Client unable to establish connection (0) (SQLDriverConnect)')
Django db configuration:
DATABASES = {
'default': {
'ENGINE': 'mssql',
'NAME': 'db_name',
'HOST': 'localhost',
'PORT': 1433,
'USER': 'sa',
'PASSWORD': 'db_pwd',
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server',
}
}
}
Installed packages:
Django==3.2.9
mssql-django==1.0
pyodbc==4.0.30
I eventually managed to solve the problem. The trouble was linked to SSL.
First, you have to open the folder /usr/local/opt. There, you can find the openssl#1.1 folder. You have to grab all of the files from there and put them into the openssl folder.
After that, I managed to run Django server successfully.
Am getting an error in making migrations for my current django project.
below is the code written in settings.py file to connect to MSSQL Server.
DATABASES = {
'default': {
'ENGINE':'sql_server.pyodbc',
'NAME':'JTPROD',
'HOST':'TZACL5X8H1N2\SQLEXPRESS', ##this is my local machine database
'USER':'xxx', ##ommitted for the post
'PASSWORD':'xxx',##ommitted for tht post
'PORT':'',
'OPTIONS':{
'provider': 'SQLOLEDB', # Have also tried 'SQLCLI11' and 'SQLCLI10'
'extra_params': 'DataTypeCompatibility=80',
'driver':'SQL Server',
},
},
}
but am getting the following error when I run "python manage.py migrate
Traceback (most recent call last): File
"C:\Users\elukamis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\base.py",
line 216, in ensure_connection
self.connect() File "C:\Users\elukamis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\base.py",
line 194, in connect
self.connection = self.get_new_connection(conn_params) File "C:\Users\elukamis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sql_server\pyodbc\base.py",
line 307, in get_new_connection
timeout=timeout) pyodbc.OperationalError: ('08001', '[08001] [Microsoft][ODBC SQL Server Driver]Neither DSN nor SERVER keyword
supplied (0) (SQLDriverConnect); [08001] [Microsoft][ODBC SQL Server
Driver]Invalid connection string attribute (0)')
The above exception was the direct cause of the following exception:
Traceback (most recent call last): File "manage.py", line 15, in
execute_from_command_line(sys.argv) File "C:\Users\elukamis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management__init__.py",
line 381, in execute_from_command_line
utility.execute() File "C:\Users\elukamis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management__init__.py",
line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv) File "C:\Users\elukamis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py",
line 316, in run_from_argv
self.execute(*args, **cmd_options) File "C:\Users\elukamis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py",
line 353, in execute
output = self.handle(*args, **options) File "C:\Users\elukamis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py",
line 83, in wrapped
res = handle_func(*args, **kwargs) File "C:\Users\elukamis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\commands\migrate.py",
line 82, in handle
executor = MigrationExecutor(connection, self.migration_progress_callback) File
"C:\Users\elukamis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\executor.py",
line 18, in init
self.loader = MigrationLoader(self.connection) File "C:\Users\elukamis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\loader.py",
line 49, in init
self.build_graph() File "C:\Users\elukamis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\loader.py",
line 212, in build_graph
self.applied_migrations = recorder.applied_migrations() File "C:\Users\elukamis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\recorder.py",
line 61, in applied_migrations
if self.has_table(): File "C:\Users\elukamis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\recorder.py",
line 44, in has_table
return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor())
File
"C:\Users\elukamis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\base.py",
line 255, in cursor
return self._cursor() File "C:\Users\elukamis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\base.py",
line 232, in _cursor
self.ensure_connection() File "C:\Users\elukamis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\base.py",
line 216, in ensure_connection
self.connect() File "C:\Users\elukamis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\utils.py",
line 89, in exit
raise dj_exc_value.with_traceback(traceback) from exc_value File "C:\Users\elukamis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\base.py",
line 216, in ensure_connection
self.connect() File "C:\Users\elukamis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\base.py",
line 194, in connect
self.connection = self.get_new_connection(conn_params) File "C:\Users\elukamis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sql_server\pyodbc\base.py",
line 307, in get_new_connection
timeout=timeout) django.db.utils.OperationalError: ('08001', '[08001] [Microsoft][ODBC SQL Server Driver]Neither DSN nor SERVER
keyword supplied (0) (SQLDriverConnect); [08001] [Microsoft][ODBC SQL
Server Driver]Invalid connection string attribute (0)')
Does anyone have an idea on what I might be missing in my configuration ?
If you're still looking for an answer, I'm not sure this will help you, but it helped me when I was getting a similar error. SQL Server Express has TCP/IP connections disabled by default, but they apparently need to be enabled in order to allow a connection like this.
Open SQL Server Configuration Manager, expand 'SQL Server Network Configuration', and click on 'Protocols for SQLEXPRESS'. Assuming the entry for TCP/IP shows as Disabled, right-click that and select Properties. On the 'Protocol' tab, set Enabled to Yes. I also updated the 'IP Addresses' tab by entering '1433' into the TCP Port field of the 'IPAll' section (at the bottom). Then I specified 'PORT':'1433', in the settings.py DATABASES configuration section. I'm not sure that was necessary, but following those steps allowed my app to successfully connect to SQL Server Express.
please i need help connecting database with jython on django and help me with the server when i put jython manage.py runserver the server don´t run
DATABASES = {
'default': {
#'ENGINE': 'doj.backends.zxjdbc.sqlite3',
'ENGINE': 'django.db.backends.mysql',
'NAME': 'tanagraweb',
'USER': 'root',
'PASSWORD': 'root',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
and django throw this:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "C:\jython2.7b3\Lib\site-packages\django\core\management\__init__.py", line 399, in execute_from_command_line
utility.execute()
File "C:\jython2.7b3\Lib\site-packages\django\core\management\__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\jython2.7b3\Lib\site-packages\django\core\management\base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "C:\jython2.7b3\Lib\site-packages\django\core\management\base.py", line 280, in execute
translation.activate('en-us')
File "C:\jython2.7b3\Lib\site-packages\django\utils\translation\__init__.py", line 130, in activate
return _trans.activate(language)
File "C:\jython2.7b3\Lib\site-packages\django\utils\translation\trans_real.py", line 188, in activate
_active.value = translation(language)
File "C:\jython2.7b3\Lib\site-packages\django\utils\translation\trans_real.py", line 177, in translation
default_translation = _fetch(settings.LANGUAGE_CODE)
File "C:\jython2.7b3\Lib\site-packages\django\utils\translation\trans_real.py", line 159, in _fetch
app = import_module(appname)
File "C:\jython2.7b3\Lib\site-packages\django\utils\importlib.py", line 40, in import_module
__import__(name)
File "C:\jython2.7b3\Lib\site-packages\django\contrib\admin\__init__.py", line 6, in <module>
from django.contrib.admin.sites import AdminSite, site
File "C:\jython2.7b3\Lib\site-packages\django\contrib\admin\sites.py", line 4, in <module>
from django.contrib.admin.forms import AdminAuthenticationForm
File "C:\jython2.7b3\Lib\site-packages\django\contrib\admin\forms.py", line 6, in <module>
from django.contrib.auth.forms import AuthenticationForm
File "C:\jython2.7b3\Lib\site-packages\django\contrib\auth\forms.py", line 17, in <module>
from django.contrib.auth.models import User
File "C:\jython2.7b3\Lib\site-packages\django\contrib\auth\models.py", line 47, in <module>
#python_2_unicode_compatible
File "C:\jython2.7b3\Lib\site-packages\django\db\models\base.py", line 96, in __new__
new_class.add_to_class('_meta', Options(meta, **kwargs))
File "C:\jython2.7b3\Lib\site-packages\django\db\models\base.py", line 264, in add_to_class
value.contribute_to_class(cls, name)
File "C:\jython2.7b3\Lib\site-packages\django\db\models\options.py", line 124, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "C:\jython2.7b3\Lib\site-packages\django\db\__init__.py", line 34, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "C:\jython2.7b3\Lib\site-packages\django\db\utils.py", line 198, in __getitem__
backend = load_backend(db['ENGINE'])
File "C:\jython2.7b3\Lib\site-packages\django\db\utils.py", line 113, in load_backend
return import_module('%s.base' % backend_name)
File "C:\jython2.7b3\Lib\site-packages\django\db\utils.py", line 113, in load_backend
return import_module('%s.base' % backend_name)
File "C:\jython2.7b3\Lib\site-packages\django\utils\importlib.py", line 40, in import_module
__import__(name)
File "C:\jython2.7b3\Lib\site-packages\django\db\backends\mysql\base.py", line 17, in <module>
raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb
i research and found that is the connector of mysql but i don´t know why this does´t work
if anyone has a project in jython on django i will be so much gratefull this is too hard
Although MySQLdb is the most widely used MySql library among the Python/Django community, you can't use it because Jython does not work with C extensions.
To use Jython with Django, you need to install django-jython and set the db backend to 'ENGINE': 'doj.db.backends.mysql'
This is documented in the official doc Running Django on Jython
Install pymysql with "pip install pymysql"
Add the following to your manage.py file:
import pymysql
pymysql.install_as_MySQLdb()
In your DATABASES setting, use:
ENGINE': 'django.db.backends.mysql',
I want to add some python codes after syncdb, so I decide to write a build.py which does everything including syncdb.
I write something in build.py as:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
from django.core.management import call_command
from django.contrib.auth.models import User
call_command('syncdb', interactive=False)
But when I run build.py, it said:
Traceback (most recent call last):
File "/home/csimstu/PycharmProjects/TeenHope/TeenHope/build.py", line 5, in <module>
call_command('syncdb', interactive=False)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 161, in call_command
return klass.execute(*args, **defaults)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 255, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 385, in handle
return self.handle_noargs(**options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/syncdb.py", line 56, in handle_noargs
cursor = connection.cursor()
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/__init__.py", line 324, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/base.py", line 306, in _cursor
self._sqlite_create_connection()
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/base.py", line 296, in _sqlite_create_connection
self.connection = Database.connect(**kwargs)
sqlite3.OperationalError: unable to open database file
I've tried ./manage.py syncdb and use call_command in interactive shell mode, and both ways worked perfectly okay. How could it be?
If the database file isn't found that's probably because you didn't use a full path in your settings.
import os
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.realpath(os.path.join(os.path.dirname(__file__), "relative_path_here", "database.db")),
Out of topic: I'd recommend to use Fabric for this kind of commands.
Just to make sure I have the setup right in my current django project, I did the following:
added south to installed_apps
manage.py syncdb (using windows)
manage.py convert_to_south app
I didn't make changes to my database yet but whether or not I do, I run:
manage.py schemamigration app --auto
and I get the following errors:
Traceback (most recent call last):
File "manage.py", line 17, in <module>
execute_from_command_line(sys.argv)
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 443, in execute
_from_command_line
utility.execute()
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python27\lib\site-packages\django\core\management\base.py", line 196, in run_from_ar
gv
self.execute(*args, **options.__dict__)
File "C:\Python27\lib\site-packages\django\core\management\base.py", line 232, in execute
output = self.handle(*args, **options)
File "C:\Python27\lib\site-packages\south\management\commands\schemamigration.py", line 98, i
n handle
old_orm = last_migration.orm(),
File "C:\Python27\lib\site-packages\south\utils.py", line 62, in method
value = function(self)
File "C:\Python27\lib\site-packages\south\migration\base.py", line 418, in orm
return FakeORM(self.migration_class(), self.app_label())
File "C:\Python27\lib\site-packages\south\orm.py", line 46, in FakeORM
_orm_cache[args] = _FakeORM(*args)
File "C:\Python27\lib\site-packages\south\orm.py", line 125, in __init__
self.models[name] = self.make_model(app_label, model_name, data)
File "C:\Python27\lib\site-packages\south\orm.py", line 318, in make_model
field = self.eval_in_context(code, app, extra_imports)
File "C:\Python27\lib\site-packages\south\orm.py", line 236, in eval_in_context
return eval(code, globals(), fake_locals)
File "<string>", line 1
SouthFieldClass(default=datetime.datetime(2012, 9, 14, 20, 46, 3, 957000, tzinfo=<UTC>))
^
SyntaxError: invalid syntax
What's going on?
It's not letting me use schemamigration. Been searching a lot of places and deleting and resetting the database and everything, can't seem to find out what the problem is.
Problem resolved:
Didn't know about south_migrationhistory location. I was always wondering about where some extra information was hanging.
Have a look here: http://south.aeracode.org/ticket/1069
Its a bug that is fixed in newer South versions. It seems you are using an obsolete version of South(<=0.7.3)