Where to store MongoClient in Django

有些话、适合烂在心里 提交于 2021-02-10 07:37:07

问题


I'm using pymongo to allow my Django site to save data to MongoDB. Apparently the MongoClient() class has connection pooling built in, and it should only be instantiated once when Django starts up. So each connection to my Django site will basically reuse that single MongoClient. I see lots of information on the web that states that this is the way it should be done.However, I cannot find any suggestions on where exactly in Django to put this single instance of MongoClient. Most Django literature says explicitly not to persist global variables used across all user sessions.

So where exactly do I create and store this single instance of MongoClient? In views.py? In models.py? Somewhere else? And if there is just a single instance of MongoClient, how exactly does the connection pooling inside help?


回答1:


It's a bit late answering this question, but future searchers may find it useful.

If you're only using MongoDB for a few operations (and thus don't want to use the full MongoEngine architecture), you can set up your architecture like this:

# project/settings.py
  (place Mongo connection info here)

# project/__init__.py
  (declare a global MongoClient here, it will be throughout the app)

# project/all apps
  import project.MY_MONGO_CLIENT_NAME
  (use MongoClient as needed)

A more full breakdown may be found here: https://gist.github.com/josephmosby/4497f8a4f675170180ab




回答2:


Further to (and inspired by) josephmosby's answer, I'm using something like the following:

# project/settings

MONGO_DB = {
    'default': {
        'HOST': 'localhost',
        'PORT': 27017
    },
    ...
}

# project/__init__.py

gMongoClient = {}

# project/utils/mongo_tool.py

from project import gMongoClient
from project.settings import MONGO_DB
import pymongo

def get_mongo_db(dbname="default"):
    if dbname in gMongoClient:
        return gMongoClient[dbname]

    if dbname in MONGO_DB:
        with MONGO_DB[dbname] as config:
            gMongoClient = pymongo.MongoClient(config["HOST"],
                                               config["PORT"])
    else:
        gMongoClient[dbname] = None

    return gMongoClient[dbname]

# .../view.py

from utils import mongo_tool
...
db = mongo_tool.get_mongo_db()
results = db["collection"].find(...)

This could be made fancier, e.g. to see if a user and password are specified in the settings for a particular connection, etc., but the above captures the essence of the idea.



来源:https://stackoverflow.com/questions/25556034/where-to-store-mongoclient-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!