Setting up django-mssql issues - django

I'm having some issues setting up django-mssql on Win Server 2008 R2. I have everything installed, however, the wiki for django-mssql says to setup the settings file similar to:
DATABASES = {
'default': {
'NAME': 'my_database',
'ENGINE': 'sqlserver_ado',
'HOST': 'dbserver\\ss2008',
'USER': '',
'PASSWORD': '',
'OPTIONS' : {
'provider': 'SQLOLEDB',
'use_mars': True,
},
}
}
When I run from my site directory:
python manage.py syncdb
I get an error stating it isn't an available database backend. When I installed django-mssql it seemed to install the backend here \site-packages\django_mssql-1.0.1-py2.7.egg\sqlserver_ado does this need to be copied to site-packages\django\db\backends?
I get the same error if I set my settings to:
DATABASES = {
'default': {
'NAME': 'my_database',
'ENGINE': 'django_mssql-1.0.1-py2.7.egg.sqlserver_ado',
'HOST': 'dbserver\\ss2008',
'USER': '',
'PASSWORD': '',
'OPTIONS' : {
'provider': 'SQLOLEDB',
'use_mars': True,
},
}
}
Am I missing something when setting up this backend? This is my first time using django, but I didn't see anything in the documentation for setting up a different backend, and the django-mssql wiki or issues doesn't seem to have anything either.
Also, if there is other documentation somewhere that can help please let me know.
EDIT: The django app is running on Ubuntu server.

Dustin's comment about making sure "import sqlserver_ado" from the command shell got me going down the right path on my Django 1.8.1, Python 3.5 Win32 system with pywin32 installed.
SPOILER ALERT This only gets my Django instance to run without errors. I haven't tested the ADO connection yet.
The first error message I got was:
No module named 'django.db.backends.util'
and I found there is a file called: django.db.backends.utils so I copied it and renamed it to django.db.backends.util (without the 's') and away went the error message!
So hoping this wasn't too harmful, I continued on this line of troubleshooting.
The next error message I got was:
File "C:\Program Files (x86)\Python35-32\lib\site-packages\sqlserver_ado\base.py", line 7, in <module>
from django.db.backends import BaseDatabaseWrapper, BaseDatabaseFeatures, BaseDatabaseValidation, BaseDatabaseClient
ImportError: cannot import name 'BaseDatabaseWrapper'
I changed line 7 in base.py to now say:
#from django.db.backends import BaseDatabaseWrapper, BaseDatabaseFeatures, BaseDatabaseValidation, BaseDatabaseClient
from django.db.backends.base.base import BaseDatabaseWrapper
from django.db.backends.base.features import BaseDatabaseFeatures
from django.db.backends.base.validation import BaseDatabaseValidation
from django.db.backends.base.client import BaseDatabaseClient
Yes, that's commenting out the bad line and adding four separate lines.
Then I got this error:
File "C:\Program Files (x86)\Python35-32\lib\site-packages\sqlserver_ado\base.py", line 18, in <module>
from .introspection import DatabaseIntrospection
File "C:\Program Files (x86)\Python35-32\lib\site-packages\sqlserver_ado\introspection.py", line 3, in
from django.db.backends import BaseDatabaseIntrospection
ImportError: cannot import name 'BaseDatabaseIntrospection'
so I changed the line 3 to now read:
from django.db.backends.base.introspection import BaseDatabaseIntrospection
and so on for creation.py:
from django.db.backends.base.creation import BaseDatabaseCreation
for operations.py:
from django.db.backends.base.operations import BaseDatabaseOperations
for schema.py:
from django.utils.log import getLogger
Hope this helps someone. Hope the ADO module actually connects to something.
-Sean

You will want to make sure that you can import "sqlserver_ado" from your python shell.
Put the folder sqlserver_ado somewhere on your PATH, I put mine in \site-packages\
Take a look at the README.txt.
The engine does want to be set to "sqlserver_ado" similar to how the settings are done on the settings sample page.

As of 2019:
I couldn't get Django MSSQL to work at all.
I switched over to django-pyodbc-azure and that works great.
Install:
pip install django-pyodbc-azure
Setup:
'ENGINE': 'sql_server.pyodbc'

You need to install the dependency PyWin32. You can install via pip or download from the python binaries page http://www.lfd.uci.edu/~gohlke/pythonlibs/

I was trying to get django_pyodbc to work, and couldn't. I was getting this error:
django.core.exceptions.ImproperlyConfigured: 'django_pyodbc' isn't an available database backend.
Try using 'django.db.backends.XXX', where XXX is one of:
u'base', u'mysql', u'oracle', u'postgresql_psycopg2', u'sqlite3'
Error was: cannot import name BaseDatabaseWrapper
I was directed to this post as a solution, so I'll post my answer here also.
I downgraded to django 1.6 instead of 1.8, and now django_pyodbc works as a database backend.

As per https://github.com/lionheart/django-pyodbc/pull/96, django_pyodbc should now work with Django 1.8. So this seems to be a good alternative to django-mssql for those requiring SQL Server 2008 R2 support.

Related

Django, using oracledb error msg that I need Oracle 19

I am confused since the documentation for oracledb clearly states that everything past 12.1 should work fine. Could someone please explain to me where I went wrong? The error was created when I tried to create migrations.
The document I am referencing is: oracledb docs
Here is the error:
django.db.utils.NotSupportedError: Oracle 19 or later is required (found 12.2.0.1.0).
And here is my databases string in my settings.py:
from pathlib import Path
import sys
import oracledb
oracledb.version = "8.3.0"
sys.modules["cx_Oracle"] = oracledb
#the above line was added because of error (django.core.exceptions.ImproperlyConfigured: Error
#loading cx_Oracle module: No module named 'cx_Oracle')
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.oracle',
'NAME': (
'(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=server123)(PORT=1521))'
'(CONNECT_DATA=(SERVICE_NAME=server.domain.com)))'
),
'USER': 'user123',
'PASSWORD': 'password',
'OPTIONS': {
'threaded': True,
},
}
}
It's a Django thing. From docs.djangoproject.com/en/4.1/ref/databases/#oracle-notes:
"Django supports Oracle Database Server versions 19c and higher."
Also see the Django 4.0 release notes
"Dropped support for Oracle 12.2 and 18c".
Try an older version of Django if you can't upgrade the DB

How to connect to snowflake database from Django framework

I'm new to Django and I'm trying to display the result that comes from a Snowflake database. I know that Django has multiple built-in database backend engines like: django.db.backends.postgresql and django.db.backends.mysql among the other few it supports.
Unfortunately, I couldn't find a proper way of configuring a database backend engine in the
settings.py
When I enter sqlalchemy or snowflake-sqlalchemy as the engine, I get this error:
Try using 'django.db.backends.XXX', where XXX is one of:
'mysql', 'oracle', 'postgresql', 'sqlite3'
My guess was to go with sqlalchemy as that's what I usually use to connect to Snowflake outside of Django but for some reason, it's not working properly.
I'd appreciate any guidance on that.
2022 update: There's now a Snowflake backend for Django funded by
Snowflake customers and implemented by Django's Tim Graham:
https://github.com/cedar-team/django-snowflake
From their docs:
Install and usage
Use the version of django-snowflake that corresponds to your version of Django. For example, to get the latest compatible release for Django 3.2.x:
pip install django-snowflake==3.2.*
The minor release number of Django doesn't correspond to the minor release number of django-snowflake. Use the latest minor release of each.
Configure the Django DATABASES setting similar to this:
DATABASES = {
'default': {
'ENGINE': 'django_snowflake',
'NAME': 'MY_DATABASE',
'SCHEMA': 'MY_SCHEME',
'WAREHOUSE': 'MY_WAREHOUSE',
'USER': 'my_user',
'PASSWORD': 'my_password',
'ACCOUNT': 'my_account',
},
}
Some of the discussion while implementing it:
https://groups.google.com/g/django-developers/c/po9dS-2h4lg/m/UeKBoL8dBgAJ?pli=1
You should install a custom Snowflake engine like the following ones. Note that, as of today, those are incomplete. Though, it should not be difficult to implement missing Django features by completing the operations.pyfile.
-> https://github.com/pricemoov/django-snowflake
or
-> https://pypi.org/project/django-snowflake-backend/
please install snowflake-connector-python .E.g. below
pip3 install snowflake-connector-python==1.8.1
Here is the code to connect from SQL Alchemy.
=====================================================================
#!/usr/bin/env python
from snowflake.sqlalchemy import URL
from sqlalchemy import create_engine
engine = create_engine(URL(
account = 'XXXX',
user = 'XXXX',
password = 'XXXXX',
database = 'XXXXXX',
schema = 'XXXXXX',
warehouse = 'XXXXX',
role='XXXXXXXX',
))
try:
connection = engine.connect()
connection.execute(
"CREATE OR REPLACE TABLE test_async(c1 TIMESTAMP_NTZ,c2 VARIANT)",_no_results=True)
finally:
connection.close()
engine.dispose()
=========================================================================

How do I get Jython-Django to work with Postgresql JDBC

I have tried to setup an instance of Django on Jython using Postgresql. I can't get Django to talk to Postgresql. Can someone help me figure out what is wrong?**
So far, I have the following system installed for development:
Windows 7
JDK7 (x86)
Jython Version 2.7b2
Django-1.6.5
django-jython 1.7.0b2(Database backends and management commands, for development underDjango/Jython)
postgresql-9.3.5-1-windows-x64.exe
Eclipse-PyDev
I was able to run the server and database, but now I am having issues with configuring Django to work with the Postgresql database.
I followed the configuration steps here:
https://pythonhosted.org/django-jython/database-backends.html#postgresql
My settings.py file has the following:
DATABASES = {
'default': {
'ENGINE': 'doj.db.backends.postgresql',
'NAME': 'gate',
'USER': 'gate',
'PASSWORD': 'test',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
When I check my class path:
C:\>echo %CLASSPATH%
C:\Program Files (x86)\Java\jdk1.7.0_67\bin.;C:\Program Files (x86)\Java\jre7\li
b\ext\QTJava.zip
I have the JDBC file postgresql-9.3-1102.jdbc41.jar in the lib folder listed above.
Error Message from Django
File "C:\jython2.7b2\Lib\site-packages\django\db\utils.py", line 131, in load_backend raise ImproperlyConfigured(error_msg)
django.core.exceptions.ImproperlyConfigured: 'doj.db.backends.postgresql' isn't an available
database backend.
Try using 'django.db.backends.XXX', where XXX is one of:
u'mysql', u'oracle', u'postgresql_psycopg2', u'sqlite3'
Error was: No module named schema
I believe the problem is that you are using the wrong version of Django.
Jython-Django is compatible only starting from 1.7.
See https://docs.djangoproject.com/en/1.6/howto/jython/

Django on Mac OS X: OperationalError 'role "_www" does not exist'. PostgreSQL or Psycopg issue?

I'm new to web development with Django, so please bear with me and thank you in advance for your help.
I've been using Django's own web server and SQLite while learning. However, now I'm struggling trying to get a more complex setup working for my next project.
My setup (Apache, Python, mod_wsgi, PostgreSQL server, psycopg and Django) should be working fine to the best of my knowledge. I created my Django project and visited localhost on my browser, where I got the "Welcome to Django! It worked!" page. However, when I tried to enable the admin and visited localhost/admin/ I get the following error (note that I can visit this page without errors using Django's own web server):
OperationalError at /admin/
FATAL: Role "_www" does not exist
Request Method: GET
Request URL: http://localhost/admin/
Django Version: 1.4.3
Exception Type: OperationalError
Exception Value: FATAL: role "_www" does not exist
Exception Location: /Library/Python/2.7/site-packages/psycopg2/__init__.py in connect, line 178
I'm guessing something is not right with PostgreSQL or psycopg, but I'm unsure what exactly. This is how I got here:
Mac OS X Mountain Lion (10.8.2) fresh install.
Installed Xcode from Mac App Store, then command line tools from Xcode's preferences.
Didn't update Apache or Python, I'm using whatever was installed by default (Apache 2.2.22 and Python 2.7.2).
Installed mod_wsgi 3.4 from source
$ curl -O http://modwsgi.googlecode.com/files/mod_wsgi-3.4.tar.gz
$ tar xvfz mod_wsgi-3.4.tar.gz
$ cd mod_wsgi-3.4
$ ./configure
$ make
(Note: I had to enter the following line for "make" to work:)
$ sudo ln -s /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/ /Applications/Xcode.app/Contents/Developer/Toolchains/OSX10.8.xctoolchain
$ make install
Edited httpd.conf, added the following and restarted Apache:
LoadModule wsgi_module libexec/apache2/mod_wsgi.so
Installed PostgreSQL 9.2.2 by downloading the Postgres.app (http://postgresapp.com) and copying it to my Applications folder.
Ran the app, added the following to my ~/.bash_profile (for Terminal to use Postgres.app's psql by default, rather than the one that comes pre-installed on Mac OS X) and then created a new database to use with my django project:
PATH="/Applications/Postgres.app/Contents/MacOS/bin:$PATH"
Installed psycopg 2.4.6 from source. I downloaded the file, unpacked it and then from the unpacked directory:
$ python setup.py install
Installed Django 1.4.3 from source. Created a new project and a new app. Edited the settings.py to uncomment the admin app and enter my database settings (as described on Postgres.app's website: http://postgresapp.com/documentation#toc_3). The database settings look as follows:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'my_db_name',
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
}
}
Edited urls.py to uncomment the admin related lines:
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
)
Added the following text at the end of https.conf:
WSGIScriptAlias / /Users/antubel/Projects/Django/antubel_com/antubel_com/wsgi.py
WSGIPythonPath /Users/antubel/Projects/Django/antubel_com
<Directory /Users/antubel/Projects/Django/antubel_com>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
Restarted Apache. Tried to load localhost/admin/ on my browser and got the error mentioned above.
Please help :)
Thanks,
Dasuevia
Because your settings file doesn't specify a user to connect to Postgres with, you're trying to connect using the user account that's running your code (and because it's being run by mod_wsgi under Apache, that user is _www).
If you're using Postgres.app to run Postgres for you, you will need to set your database USERNAME value to the username for your account on the system - so if your username was foo, set USERNAME to foo like so:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'my_db_name',
'USER': 'foo',
'PASSWORD': '',
'HOST': '',
'PORT': '',
}
}
You can also update your apache config to run your code as a different user - take a look at WSGIDaemonProcess in the mod_wsgi docs if you'd like to get that going instead.

Django MongoDB Engine connection failure

[SOLVED] After successfully completing the django tutorial, I have tried to use mongoDB as a database, with Django MongoDB Engine. This is the database configuration in settings.py:
DATABASES = {
'default': {
'ENGINE': 'django_mongodb_engine',
'NAME': 'test',
'HOST': 'localhost',
'PORT': 27017,
# 'OPTIONS' : {
# 'slave_okay' : True,
# }
}
}
And this is the error message I get, after running python manage.py syncdb
:
raise AutoReconnect("could not find master/primary")
pymongo.errors.AutoReconnect: could not find master/primary
I got this error, even after shutting down the mongoDB server, so I figured out that this needed to be a connection issue. I posted an answer on how I solved this.
Using Ubuntu 12.04 LTS x64, Python 2.7, django 1.4.2, mongoDB x86_64 2.2.1 (clean new install)
The way I solved this is so simple that I am almost ashamed at myself for asking the question in the first place, but I hope it will be useful:
I simply upgraded the Django MongoDB Engine by typing into the terminal:
sudo pip install git+https://github.com/django-nonrel/mongodb-engine --upgrade
And that's it.
Can you try running ...
db.repairDatabase()
... from the mongo terminal?