7

I am trying to connect my django instance to a mongo db cluster using django. I have checked from various sources and the way it is getting closer to work is:

  • Install dnspython
  • Have the following DATABASES dict in settings.py
DATABASES = {
    'default': {
        'ENGINE': 'djongo',
        'NAME': 'test',
        'HOST': 'mongodb+srv://mongo_usr:' + urllib.parse.quote('mypassword') + '@domain_assigned.mongodb.net/test?ssl=true&ssl_cert_reqs=CERT_NONE&retryWrites=true',
        'ENFORCE_SCHEMA': False
    }
}

It truly finds the endpoint but I am getting a weird error:

pymongo.errors.ServerSelectionTimeoutError: connection closed,connection closed,connection closed

has anyone fixed this before?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
py_script
  • 808
  • 2
  • 16
  • 38
  • Possible duplicate of [Use pymongo in django directly](https://stackoverflow.com/questions/31552914/use-pymongo-in-django-directly) – markwalker_ Apr 05 '19 at 22:02
  • You can make the connection using pymongo, no need of using djongo – Andrés Aviña Apr 05 '19 at 23:10
  • Indeed, but in such case don't you bypass the orm completely? – py_script Apr 06 '19 at 14:34
  • are you using djongo in production environment ? – Pratibha May 06 '19 at 08:55
  • For now it is in dev, but the ultimate goal is to move it live. It is about a personal project, not a corporate one. I wouldnt try it on production for my company. Not because it is not of high quality but because I am afraid of one man projects (there are many contributors though) – py_script May 07 '19 at 18:53

7 Answers7

8

I just setup Djongo and MongoDB Atlas with the following:

DATABASES = {
        'default': {
        'ENGINE': 'djongo',
        'NAME': '<db name>',
        'HOST': 'mongodb+srv://<db username>:<db password>@....mongodb.net/test?retryWrites=true',
        'USER': '<db username>',
        'PASSWORD': '<db password>',
    }
}

Hope that helps!

  • Make sure you have Djongo installed. "pip install djongo". – Market Ahead Apr 07 '19 at 15:07
  • Pretty similar to mine...though the devil hides in details. I will check and let you know – py_script Apr 07 '19 at 16:41
  • I have applied the template you posted, added my credentials and values and is still not working. I have also tried with escaping my password. Still nothing. Just to make sure, could you post a screenshot of where the db name is seen, to verify I am not putting a wrong dbname (and maybe I am putting the cluster). Thanks – py_script Apr 09 '19 at 19:20
  • any idea on how to connect it using *mongoengine* ?? – Simone Mar 05 '20 at 21:38
  • I was not able to get this method to work - even with an atlas generated password without funky characters. I noticed djongo was uninstalling my django installed build of 3.1 and installing django to 3.0.5 - not sure if this is the cause but method described by Nagarjun worked for me. – user3507825 Aug 26 '20 at 22:39
6

Install djongo package using pip install djongo.

Make sure you import following module:

import urllib

Setup database settings.

DATABASES = {
    'default': {
        'ENGINE': 'djongo',
        'NAME': '<db_name>',
        'HOST': "mongodb+srv://<db_username>:" +
                urllib.parse.quote_plus("<db_password>") +
                "@........mongodb.net/test?retryWrites=true&ssl=true&ssl_cert_reqs=CERT_NONE&w=majority",
    }
}

Substitute db_username, db_name and db_password with your credentials.

Also edit the host name given by Mongo Atlas.

RoboMex
  • 828
  • 12
  • 13
6

Got this working without any hack as follows:

  1. Install pip install dnspython.
  2. Update settings.py with the following,
'default': {
        'ENGINE': 'djongo',
        'NAME': '<dbname>',
        'CLIENT': {
            'host': "mongodb+srv://<username>:" + quote_plus('<password>') + "@<cluster-name>.mongodb.net/test?retryWrites=true&w=majority"
        },   
    }
  1. Run python manage.py makemigrations
  2. Run python manage.py migrate
Nagarjun
  • 75
  • 1
  • 10
  • I tried the checked answer but had lots of difficulties. After an hour of debugging, I tried this and it worked first go! I already had dnspython for other projects. Thanks :D – user3507825 Aug 26 '20 at 22:41
1

Simplest and correct solution:

Step 1: Install djongo and dnspython

pip install djongo

pip install dnspython

Step 2: Changes in settings.py:

DATABASES = {
"default": {
    "ENGINE": "djongo",
    "CLIENT": {
        "host": "mongodb+srv://<username>:<password>@<cluster_name>.mongodb.net/?retryWrites=true&w=majority",
        "username": "<username>",
        "password": "<password>",
        "name": "<database_name>",
        "authMechanism": "SCRAM-SHA-1",
    },
}}
Community
  • 1
  • 1
ramesh
  • 21
  • 3
0

I have managed to connect to mongo atlas with djongo by using the snippet from @Market Ahead here

It looks like they dont want the password to have weird characters inside. In such case, even escaping is not working optimally.

py_script
  • 808
  • 2
  • 16
  • 38
0

After editing your DB's settings.py, go to your env lib and make these changes in pymongo/mongo_client.py

HOST = "mongodb+srv://<Username>:<password>@cluster0-gbdot.mongodb.net/<databaseName>?retryWrites=true&w=majority"
Alexpandiyan Chokkan
  • 1,025
  • 1
  • 10
  • 30
0

For djongo version 1.3.6 this worked for me:

DATABASES = {
"default": {
    "ENGINE": "djongo",
    'ENFORCE_SCHEMA': False,
    "NAME": "<db_name>",  # name of your DB which you want to access
    "CLIENT": {
        'host': 'mongodb://<username>:<password>@<db_url>:<port>',  # your db_url if not hosted then localhost
        'port': <port>,  # port e.g. 27017
        'username': '<username>',
        'password': '<password>',
        'authSource': 'admin',  # set your db auth_source if you know
        'authMechanism': 'SCRAM-SHA-1'  # set your auth_mechanism if you know
# add other settings as per your requirements
    }
}

}