[Answered ]-Django with mongodb using pymongo without using ORM

2👍

If you don’t want to use the ORM then you can delete or comment out DATABASE = {} in settings.py.
A solution would be to create a connector that uses pymongo. Example of mongodb_connector.py:

from pymongo import MongoClient
from bson.json_util import dumps

class MongoConnector:
    username = 'user'
    password = 'pass'
    host = 'localhost'
    port = '27017'
    url = host + ':' + port
    client = MongoClient(mongo_url,username=username,password=password,authSource='admin',authMechanism='SCRAM-SHA-256')
    def find(self,db, collection, name):
        db = client[db]
        collection = db[collection]
        response = collection.find_one({"name":str(name)})
        return dumps(response)

Now you can get an object with:

from mongo_connector import MongoConnector
mongo = MongoConnector()
doc = mongo.find('mydb','mycollection','name')

Leave a comment