How to connect Django ORM to mongo atlas?
Asked Answered
S

7

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?

Skaw answered 5/4, 2019 at 21:38 Comment(5)
Possible duplicate of Use pymongo in django directlyAmando
You can make the connection using pymongo, no need of using djongoPhilanthropy
Indeed, but in such case don't you bypass the orm completely?Skaw
are you using djongo in production environment ?Misogynist
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)Skaw
L
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!

Lockridge answered 7/4, 2019 at 15:6 Comment(5)
Make sure you have Djongo installed. "pip install djongo".Lockridge
Pretty similar to mine...though the devil hides in details. I will check and let you knowSkaw
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). ThanksSkaw
any idea on how to connect it using mongoengine ??Oates
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.Supporting
G
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.

Grandchild answered 1/11, 2019 at 14:25 Comment(0)
T
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
Totally answered 10/5, 2020 at 18:39 Comment(1)
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 :DSupporting
L
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",
    },
}}
Latanya answered 1/6, 2020 at 7:13 Comment(0)
S
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.

Skaw answered 14/4, 2019 at 13:33 Comment(1)
You accepted your own answer referencing the actual answerSyncopated
D
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"
Decanal answered 4/2, 2021 at 12:40 Comment(0)
T
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
    }
}

}

Thecla answered 13/7, 2022 at 12:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.