Created
January 18, 2011 04:37
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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