Skip to content

Instantly share code, notes, and snippets.

@dmpayton
Created January 18, 2011 04:37
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save dmpayton/783993 to your computer and use it in GitHub Desktop.
Save dmpayton/783993 to your computer and use it in GitHub Desktop.
Django middleware to ensure that a separate mongoengine connection is made per thread.
import mongoengine
from django.conf import settings
from django.core.exceptions import MiddlewareNotUsed
class MongoEngineConnectionMiddleware(object):
''' Ensure that a separate mongoengine connection is made per thread.
See: http://groups.google.com/group/mongoengine-users/browse_thread/thread/1aa3f9d65627c04
Assumes the following is your Django settings. Tweak as needed.
MONGODB = {
'db': '<database_name>',
'options': {
'host': '<host>',
'port': <port>,
'username': '<username>',
'password': '<password>'
}
}
'''
def __init__(self):
mongoengine.connect(settings.MONGODB['db'], **settings.MONGODB['options'])
raise MiddlewareNotUsed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment