djongo, unable to connect to remote db on cloud.mongodb.com - python

i think below setting will work for the djongo to connect to the remote mongodb on mongodb.com but, the error message shows its still trying to connect to the localhost
DATABASES = {
'default': {
'ENGINE': 'djongo',
'HOST': 'mongodb+srv://<username>:<password>#cluster-name/<dbname>?retryWrites=true&w=majority',
}
below is the error traceback
Traceback (most recent call last):
File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "/usr/lib/python3.6/threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "venv/lib/python3.6/site-packages/django/utils/autoreload.py", line 54, in wrapper
fn(*args, **kwargs)
File "venv/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 120, in inner_run
self.check_migrations()
File "venv/lib/python3.6/site-packages/django/core/management/base.py", line 453, in check_migrations
executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
File "venv/lib/python3.6/site-packages/django/db/migrations/executor.py", line 18, in __init__
self.loader = MigrationLoader(self.connection)
File "venv/lib/python3.6/site-packages/django/db/migrations/loader.py", line 49, in __init__
self.build_graph()
File "venv/lib/python3.6/site-packages/django/db/migrations/loader.py", line 212, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "venv/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 73, in applied_migrations
if self.has_table():
File "venv/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 56, in has_table
return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor())
File "venv/lib/python3.6/site-packages/django/db/backends/base/introspection.py", line 48, in table_names
return get_names(cursor)
File "venv/lib/python3.6/site-packages/django/db/backends/base/introspection.py", line 43, in get_names
return sorted(ti.name for ti in self.get_table_list(cursor)
File "venv/lib/python3.6/site-packages/djongo/introspection.py", line 47, in get_table_list
for c in cursor.db_conn.list_collection_names()
File "venv/lib/python3.6/site-packages/pymongo/database.py", line 856, in list_collection_names
for result in self.list_collections(session=session, **kwargs)]
File "venv/lib/python3.6/site-packages/pymongo/database.py", line 819, in list_collections
_cmd, read_pref, session)
File "venv/lib/python3.6/site-packages/pymongo/mongo_client.py", line 1454, in _retryable_read
read_pref, session, address=address)
File "venv/lib/python3.6/site-packages/pymongo/mongo_client.py", line 1253, in _select_server
server = topology.select_server(server_selector)
File "venv/lib/python3.6/site-packages/pymongo/topology.py", line 235, in select_server
address))
File "venv/lib/python3.6/site-packages/pymongo/topology.py", line 193, in select_servers
selector, server_timeout, address)
File "venv/lib/python3.6/site-packages/pymongo/topology.py", line 209, in _select_servers_loop
self._error_message(selector))
pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused

I have met your problem before. In my case, I run django & mongodb inside containers. And I found that whatever setting I set inside setting.py, the djongo(they use pymongo behind) just ignore my setting.
For others seeking for the solution, you may want to check your syntax in setting.py which should look like below:
DATABASES = {
'default': {
'ENGINE': 'djongo',
'NAME': 'your-db-name',
'ENFORCE_SCHEMA': False,
'CLIENT': {
'host': 'host-name or ip address',
'port': port_number,
'username': 'db-username',
'password': 'password',
'authSource': 'db-name',
'authMechanism': 'SCRAM-SHA-1'
},
.......
}
Detail from djongo document
You can see the host, user, password fields are all under 'CLIENT' , which is different with the default syntax described by Django. So maybe most people get confused here and stuck at this problem.

Go to settings.py file and use below Database settngs
DATABASES = {
'default': {
'ENGINE': 'djongo',
'ENFORCE_SCHEMA': True
'NAME': 'your-db-name',
'HOST': 'host-name or ip address',
'PORT': port_number,
'USER': 'db-username',
'PASSWORD': 'password',
'AUTH_SOURCE': 'db-name',
'AUTH_MECHANISM': 'SCRAM-SHA-1',
}
Please also check out this article for connection between django and mongo db.
https://medium.com/#ksarthak4ever/how-to-use-django-with-mongodb-40ba36a21124
I personally recommend you please use pycharm. In Pycharm you can test your connection. So that you can get more clear image of whole scenario.
I use remote database in django. you can see in the below picture

I GOT THE SOLUTION
I adopted following approach >
so, behind the scene the djongo uses -> pymongo
and pymongo's default configuration are
class MongoClient(common.BaseObject):
HOST = "localhost" # here HOST has the hardcoded value
PORT = 27017
which lies in following file
venv/lib/python3.6/site-packages/pymongo/mongo_client.py
replace the harcoded value of HOST to something like
HOST = 'mongodb+srv://<username>:<password>#cluster-name/<dbname>?retryWrites=true&w=majority'
ADDITIONALLY
We can set the environment variable if we want
HOST = os.getenv('MONGO_DB_URL')

Related

django.core.exceptions.ImproperlyConfigured: The database name '****.amazonaws.com' (85 characters) is longer than PostgreSQL's limit of 63 characters

I am trying to migrate my Django model to the AWS Lightsail database, but I am getting below error. My database is on AWS Lightsail. it is PostgreSQL. I am deploying my project on AWS so I have set up almost everything except model migration.
AMAZON DATABASE AVAILABLE CONNECTION OPTIONS:
Endpoint-> ************.ap-******-1.***.amazonaws.com,
Port-> 5444,
User name-> db,
Password-> Something
MY SETTINGS.PY FILE DATABASE CONNECTION OPTIONS:
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': '****Endpoint*****',
'USER': 'db',
'PASSWORD': 'Something',
'HOST': '5444'
}
}
Terminal Error:
> `2, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "/home/bitnami/portfolio/ManojJha-portfolio/.venv/lib/python3.7/site-packages/django/db/migrations/recorder.py", lin
e 76, in applied_migrations
if self.has_table():
File "/home/bitnami/portfolio/ManojJha-portfolio/.venv/lib/python3.7/site-packages/django/db/migrations/recorder.py", lin
e 56, in has_table
return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor())
File "/home/bitnami/portfolio/ManojJha-portfolio/.venv/lib/python3.7/site-packages/django/utils/asyncio.py", line 26, in
inner
return func(*args, **kwargs)
File "/home/bitnami/portfolio/ManojJha-portfolio/.venv/lib/python3.7/site-packages/django/db/backends/base/base.py", line
260, in cursor
return self._cursor()
File "/home/bitnami/portfolio/ManojJha-portfolio/.venv/lib/python3.7/site-packages/django/db/backends/base/base.py", line
236, in _cursor
self.ensure_connection()
File "/home/bitnami/portfolio/ManojJha-portfolio/.venv/lib/python3.7/site-packages/django/utils/asyncio.py", line 26, in
inner
return func(*args, **kwargs)
File "/home/bitnami/portfolio/ManojJha-portfolio/.venv/lib/python3.7/site-packages/django/db/backends/base/base.py", line
220, in ensure_connection
self.connect()
File "/home/bitnami/portfolio/ManojJha-portfolio/.venv/lib/python3.7/site-packages/django/utils/asyncio.py", line 26, in
inner
return func(*args, **kwargs)
File "/home/bitnami/portfolio/ManojJha-portfolio/.venv/lib/python3.7/site-packages/django/db/backends/base/base.py", line
196, in connect
conn_params = self.get_connection_params()
File "/home/bitnami/portfolio/ManojJha-portfolio/.venv/lib/python3.7/site-packages/django/db/backends/postgresql/base.py"
, line 165, in get_connection_params
self.ops.max_name_length(),
django.core.exceptions.ImproperlyConfigured: The database name '*****************Database End-point*****************.***.amazonaws.com' (85 characters) is longer than PostgreSQL's limit of 63 characters. Supply a shorter NAME in settings.DATABASES.```
So is there any way to shorten the endpoint name or there is any other solution to handle this error. Please guide me to set up this.
Thanks,
-Manoj Jha
"HOST" should be the hostname, not the port!
"NAME" is the name of the database.
An example from the Django documentation:
'ENGINE': 'django.db.backends.oracle',
'NAME': 'xe',
'USER': 'a_user',
'PASSWORD': 'a_password',
'HOST': 'dbprod01ned.mycompany.com',
'PORT': '1540',

403 Missing or insufficient permissions

I am running datastore emulator. When I run "dev_appserver.py app.yaml" command, I get this error
403 Missing or insufficient permissions.
This is the warning I get. I know there is a problem with authentication. I have gone through this. But couldn't end up finding a solution.
Some details:
I am using MySQL databases for some apps.
For others I want to use datastore.
I am running datastore emulator on one tab, Google cloud proxy on another, dev_appserver on the third one.
I have set the environment variables using the command gcloud beta emulators datastore env-init.
My settings.py
if os.getenv('GAE_APPLICATION', None):
# Running on production App Engine, so connect to Google Cloud SQL using
# the unix socket at /cloudsql/<your-cloudsql-connection string>
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'HOST': '/cloudsql/connectionname',
'NAME': 'db_name',
'USER': 'username',
'PASSWORD': 'password',
}
}
else:
# Running locally so connect to either a local MySQL instance or connect to
# Cloud SQL via the proxy. To start the proxy via command line:
#
# $ cloud_sql_proxy -instances=[INSTANCE_CONNECTION_NAME]=tcp:3306
#
# See https://cloud.google.com/sql/docs/mysql-connect-proxy
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'HOST': '127.0.0.1',
'PORT': '3307',
'NAME': 'dbname',
'USER': 'username',
'PASSWORD': 'password',
}
}
# [END db_setup]
My app.yaml
runtime: python37
handlers:
- url: /static
static_dir: static/
- url: /.*
script: auto
Technology background:
Django 2.1
Python 3.5.2
Stack trace:
Traceback (most recent call last):
File "/tmp/tmpIZrOSY/lib/python3.5/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/tmp/tmpIZrOSY/lib/python3.5/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/tmp/tmpIZrOSY/lib/python3.5/site-packages/django/core/handlers/base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/salman/chaipani_env/project_chaipani/chaipani/views.py", line 20, in post_new
post = models.insert(data)
File "/home/salman/chaipani_env/project_chaipani/chaipani/models.py", line 15, in insert
client.put(entity)
File "/tmp/tmpIZrOSY/lib/python3.5/site-packages/google/cloud/datastore/client.py", line 404, in put
self.put_multi(entities=[entity])
File "/tmp/tmpIZrOSY/lib/python3.5/site-packages/google/cloud/datastore/client.py", line 431, in put_multi
current.commit()
File "/tmp/tmpIZrOSY/lib/python3.5/site-packages/google/cloud/datastore/batch.py", line 273, in commit
self._commit()
File "/tmp/tmpIZrOSY/lib/python3.5/site-packages/google/cloud/datastore/batch.py", line 249, in _commit
self.project, mode, self._mutations, transaction=self._id)
File "/tmp/tmpIZrOSY/lib/python3.5/site-packages/google/cloud/datastore_v1/gapic/datastore_client.py", line 426, in commit
request, retry=retry, timeout=timeout, metadata=metadata)
File "/tmp/tmpIZrOSY/lib/python3.5/site-packages/google/api_core/gapic_v1/method.py", line 139, in __call__
return wrapped_func(*args, **kwargs)
File "/tmp/tmpIZrOSY/lib/python3.5/site-packages/google/api_core/retry.py", line 260, in retry_wrapped_func
on_error=on_error,
File "/tmp/tmpIZrOSY/lib/python3.5/site-packages/google/api_core/retry.py", line 177, in retry_target
return target()
File "/tmp/tmpIZrOSY/lib/python3.5/site-packages/google/api_core/timeout.py", line 206, in func_with_timeout
return func(*args, **kwargs)
File "/tmp/tmpIZrOSY/lib/python3.5/site-packages/google/api_core/grpc_helpers.py", line 61, in error_remapped_callable
six.raise_from(exceptions.from_grpc_error(exc), exc)
File "<string>", line 3, in raise_from
google.api_core.exceptions.PermissionDenied: 403 Missing or insufficient permissions.
INFO 2018-10-10 10:51:26,298 module.py:880] default: "POST /users/post/new/ HTTP/1.1" 500 133823
It seems that your Default service account does not have enough permissions. It might require some additional permissions such as "Datastore Index Admin" to infer over a Datastore[1]. I am not sure on how this will work in the Datastore emulator, but since it aims to provide a local emulation, maybe it will get the roles and permission to perform a test. Could you please check the permissions in your service account[2], change if needed and let me know if the issue persists. I will be waiting for your reply.
[1]https://cloud.google.com/appengine/docs/flexible/nodejs/granting-project-access#before_you_begin
[2]https://cloud.google.com/iam/docs/granting-roles-to-service-accounts

Access error in database PostgreSQL

Edit skip first part, I found out (well, they tell me) that the problem is in the database \edit
I Have a python+django project that I keep on two computers. I mean have two copy of the same project and sometimes I work on one, sometimes on the other. With copy-paste I lose the secret key in a settings.py (they were different but I overwrite). I don't know how generation and storage of secret keys works but to recover I start a new project, took the key and copied the files like this:
1) make a copy of the project
2) deleted the original project but kept the copy
3) started a new project with the same name (so, in the same folder like the original)
4) copied the new secret key
5) copied the files from the copy of the old project in the new project (so settings.py is lost and the new secret key with it, but I have a copy)
6) changed the secret key with the new secret key
But this don't works and it gives the same error like before:
System check identified no issues (0 silenced).
Unhandled exception in thread started by <function check_errors.<locals>.wrapper
at 0x0000000004ADAEA0>
Traceback (most recent call last):
File "C:\Users\Fabio\Envs\possedimenti\lib\site-packages\django\db\backends\ba
se\base.py", line 216, in ensure_connection
self.connect()
File "C:\Users\Fabio\Envs\possedimenti\lib\site-packages\django\db\backends\ba
se\base.py", line 194, in connect
self.connection = self.get_new_connection(conn_params)
File "C:\Users\Fabio\Envs\possedimenti\lib\site-packages\django\db\backends\po
stgresql\base.py", line 174, in get_new_connection
connection = Database.connect(**conn_params)
File "C:\Users\Fabio\Envs\possedimenti\lib\site-packages\psycopg2\__init__.py"
, line 130, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: FATALE: autenticazione con password fallita per l'ut
ente "gm"
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\Fabio\Envs\possedimenti\lib\site-packages\django\utils\autorelo
ad.py", line 225, in wrapper
fn(*args, **kwargs)
File "C:\Users\Fabio\Envs\possedimenti\lib\site-packages\django\core\managemen
t\commands\runserver.py", line 120, in inner_run
self.check_migrations()
File "C:\Users\Fabio\Envs\possedimenti\lib\site-packages\django\core\managemen
t\base.py", line 442, in check_migrations
executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
File "C:\Users\Fabio\Envs\possedimenti\lib\site-packages\django\db\migrations\
executor.py", line 18, in __init__
self.loader = MigrationLoader(self.connection)
File "C:\Users\Fabio\Envs\possedimenti\lib\site-packages\django\db\migrations\
loader.py", line 49, in __init__
self.build_graph()
File "C:\Users\Fabio\Envs\possedimenti\lib\site-packages\django\db\migrations\
loader.py", line 209, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "C:\Users\Fabio\Envs\possedimenti\lib\site-packages\django\db\migrations\
recorder.py", line 61, in applied_migrations
if self.has_table():
File "C:\Users\Fabio\Envs\possedimenti\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\Fabio\Envs\possedimenti\lib\site-packages\django\db\backends\ba
se\base.py", line 255, in cursor
return self._cursor()
File "C:\Users\Fabio\Envs\possedimenti\lib\site-packages\django\db\backends\ba
se\base.py", line 232, in _cursor
self.ensure_connection()
File "C:\Users\Fabio\Envs\possedimenti\lib\site-packages\django\db\backends\ba
se\base.py", line 216, in ensure_connection
self.connect()
File "C:\Users\Fabio\Envs\possedimenti\lib\site-packages\django\db\utils.py",
line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\Fabio\Envs\possedimenti\lib\site-packages\django\db\backends\ba
se\base.py", line 216, in ensure_connection
self.connect()
File "C:\Users\Fabio\Envs\possedimenti\lib\site-packages\django\db\backends\ba
se\base.py", line 194, in connect
self.connection = self.get_new_connection(conn_params)
File "C:\Users\Fabio\Envs\possedimenti\lib\site-packages\django\db\backends\po
stgresql\base.py", line 174, in get_new_connection
connection = Database.connect(**conn_params)
File "C:\Users\Fabio\Envs\possedimenti\lib\site-packages\psycopg2\__init__.py"
, line 130, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: FATALE: autenticazione con password fallita p
er l'utente "gm"
translated: authentication with password failed for user 'gm'
I'm not sure when it happened the first time but I have another problem, probabily linked: I can't access the database. Maybe this is the cause of the first problem and not the secret key.
I use PostgreSQL and I access it using the utility standard pgAdmin III. It gives me the initial screen with Servers: PostgreSQL 9.5, I right click on it and select connect, it ask me for password, I gives but it throws an error:
An error has occurred. Error connecting to the server: FATALE: autenticazione con password fallita per l'utente 'postgres'.
translated: authentication with password failed for user 'postgres'
I have created only one database and user so I have the standard postgres and my database 'possedimenti_db' with an user called 'gm'.
What can I do? I don't mind the data present, I can delete all but if it don't make me access I don't know how to delete.
Thank you
My settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'possedimenti_db',
'USER': 'gm',
'PASSWORD': database_key,
'HOST': host_key,
'PORT': '5432',
}
}
database_key and host_key are imported from external file.
Also database_key is different from the password I use to access the database with pgAdmin. In my other pc it works. I tried to change it in the other password but it throws anyway the same error.
Edit
You Can read the solution in the comments of the answer but the problem was that my user hadn't a password definited.
Use 'django.db.backends.postgresql_psycopg2' instead of 'django.db.backends.postgresql'.
you have to install psycopg2. Use this command:
pip install psycopg2
Here is the example:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'team_db',
'USER': 'jason',
'PASSWORD': 'abcd',
'HOST': 'localhost',
'PORT': '5432',
}
}

Microsoft SQL Server 2008: Write to the server failed

I have a Django app with a worker process which does database transactions. Under the hood, I have an Azure SQL database attached to my application. The worker code looks something like this:
class Command(BaseCommand):
def handle(self, *args, **kwargs):
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')
django.setup()
consumer = Consumer()
logging.info('Booting drive consumer')
while True:
messages = consumer.poll()
for message in messages:
...
DriveEvent.objects.create(event_id=response['Id'], drive_id=drive_payload['drive_id'])
...
After about 20 mins, the create db object calls start failing with the following error:
[drive-consumer-1]2017-07-19T03:19:08.545128400Z Traceback (most recent call last):
[drive-consumer-1]2017-07-19T03:19:08.545133000Z File "/code/miles/management/commands/drive_consumer_worker.py", line 28, in handle
[drive-consumer-1]2017-07-19T03:19:08.545137900Z drive_consumer.consume(json.loads(record.value))
[drive-consumer-1]2017-07-19T03:19:08.545142500Z File "/code/miles/workers/drive_consumer.py", line 35, in consume
[drive-consumer-1]2017-07-19T03:19:08.545152200Z self._on_create(message['calendar_id'], drive_payload, message['access_token'])
[drive-consumer-1]2017-07-19T03:19:08.545156900Z File "/code/miles/workers/drive_consumer.py", line 46, in _on_create
[drive-consumer-1]2017-07-19T03:19:08.545161500Z DriveEvent.objects.create(event_id=response['Id'], drive_id=drive_payload['drive_id'])
[drive-consumer-1]2017-07-19T03:19:08.545166500Z File "/usr/local/lib/python2.7/site-packages/django/db/models/manager.py", line 85, in manager_method
[drive-consumer-1]2017-07-19T03:19:08.545171300Z return getattr(self.get_queryset(), name)(*args, **kwargs)
[drive-consumer-1]2017-07-19T03:19:08.545175900Z File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 399, in create
[drive-consumer-1]2017-07-19T03:19:08.545180700Z obj.save(force_insert=True, using=self.db)
[drive-consumer-1]2017-07-19T03:19:08.545185400Z File "/usr/local/lib/python2.7/site-packages/django/db/models/base.py", line 796, in save
[drive-consumer-1]2017-07-19T03:19:08.545190200Z force_update=force_update, update_fields=update_fields)
[drive-consumer-1]2017-07-19T03:19:08.545194800Z File "/usr/local/lib/python2.7/site-packages/django/db/models/base.py", line 821, in save_base
[drive-consumer-1]2017-07-19T03:19:08.545211400Z with transaction.atomic(using=using, savepoint=False):
[drive-consumer-1]2017-07-19T03:19:08.545215800Z File "/usr/local/lib/python2.7/site-packages/django/db/transaction.py", line 184, in __enter__
[drive-consumer-1]2017-07-19T03:19:08.545220400Z connection.set_autocommit(False, force_begin_transaction_with_broken_autocommit=True)
[drive-consumer-1]2017-07-19T03:19:08.545224900Z File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 391, in set_autocommit
[drive-consumer-1]2017-07-19T03:19:08.545229600Z self._set_autocommit(autocommit)
[drive-consumer-1]2017-07-19T03:19:08.545234000Z File "/usr/local/lib/python2.7/site-packages/sql_server/pyodbc/base.py", line 453, in _set_autocommit
[drive-consumer-1]2017-07-19T03:19:08.545238600Z self.connection.autocommit = autocommit
[drive-consumer-1]2017-07-19T03:19:08.545243100Z File "/usr/local/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
[drive-consumer-1]2017-07-19T03:19:08.545247700Z six.reraise(dj_exc_type, dj_exc_value, traceback)
[drive-consumer-1]2017-07-19T03:19:08.545252200Z File "/usr/local/lib/python2.7/site-packages/sql_server/pyodbc/base.py", line 453, in _set_autocommit
[drive-consumer-1]2017-07-19T03:19:08.545256800Z self.connection.autocommit = autocommit
[drive-consumer-1]2017-07-19T03:19:08.545262600Z Error: ('08S01', '[08S01] [FreeTDS][SQL Server]Write to the server failed (20006) (SQLSetConnectAttr)')
Database init (in settings.py) looks something like this:
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': os.environ.get('DB_NAME'),
'HOST': os.environ.get('DB_HOST'),
'PORT': '1433',
'USER': os.environ.get('DB_USER'),
'PASSWORD': os.environ.get('DB_PASSWORD'),
'OPTIONS': {
'host_is_server': True,
'driver_supports_utf8': True,
'extra_params': 'tds_version=7.2;'
}
}
}
Per my experience, it seems to be caused by the tds_version that the 7.2 version does not support Azure SQL Database. Please refer to my answer for the other SO thread pymssql: Connection to the database only works sometimes to change to 7.3.
Possibly, the third-party project michiya/django-pyodbc-azure on GitHub may help for you to get the more details.
Hope it helps.

Trouble connecting to MS SQL Server with django-mssql

I'm trying to use django-mssql to connect to MS SQL Server 2008 R2 with Django 1.4.2 These are my Database settings:
DATABASE_ENGINE = 'sqlserver_ado'
DATABASE_NAME = 'dbtest'
DATABASE_USER = 'App'
DATABASE_PASSWORD = '*********'
DATABASE_HOST = 'localhost'
DATABASE_OPTIONS = {
'provider': 'SQLNCLI10',
'extra_params': 'DataTypeCompatibility=80;MARS Connection=True;',
}
DATABASES = {
'default': {
'ENGINE': DATABASE_ENGINE,
'NAME': DATABASE_NAME,
'USER': DATABASE_USER,
'PASSWORD': DATABASE_PASSWORD,
'HOST': DATABASE_HOST,
'OPTIONS' : DATABASE_OPTIONS,
},
}
This is the error I get when I try to syncdb
Traceback (most recent call last):
File "C:\Python27\DataSatellite\manage.py", line 11, in <module>
execute_manager(settings)
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 459, in execute_manager
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_argv
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\django\core\management\base.py", line 371, in handle
return self.handle_noargs(**options)
File "C:\Python27\lib\site-packages\django\core\management\commands\syncdb.py", line 57, in handle_noargs
cursor = connection.cursor()
File "C:\Python27\lib\site-packages\django\db\backends\__init__.py", line 306, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "C:\Python27\lib\site-packages\sqlserver_ado\base.py", line 193, in _cursor
self.__connect()
File "C:\Python27\lib\site-packages\sqlserver_ado\base.py", line 168, in __connect
use_transactions=self.use_transactions,
File "C:\Python27\lib\site-packages\sqlserver_ado\dbapi.py", line 151, in connect
raise OperationalError(e, "Error opening connection: " + connection_string)
sqlserver_ado.dbapi.OperationalError: (AttributeError("'module' object has no attribute 'VARIANT'",), 'Error opening connection: DATA SOURCE=localhost;Initial Catalog=dbtest;UID=App;PWD=*********;PROVIDER=SQLNCLI10;MARS Connection=True;DataTypeCompatibility=80;MARS Connection=True;')
Finished "C:\Python27\DataSatellite\manage.py syncdb" execution.
I've looked everywhere and I cannot seem to understand and fix the problem. I hope someone can help!
Thanks!
Edit:
I've already created the database. I've also connected to the database using django-pyodbc, and I've successfully read and written from the database. But django-pyodbc causes problems when I use Apache, which was why I decided to try django-mssql. However, I do not understand the error it comes up with.
My Django (1.4.2) and Python (2.7) installs run on Windows, and I'm using an Apache webserver.
Update pywin32 to build 217 or greater. I found another question on stackoverflow with the same issue (on another topic):
Python COM server throws 'module' object has no attribute 'VARIANT'

Categories