I'm not using MongoEngine, so I don't know about the particulars. I am
however using PyMongo, and I'm using a different setup from the cookbook.
I setup the mongo connection as part of the RootFactory object (the one in
config that returns the root resource)
When creating the root object, the root factory adds the already existing
connection to the root resource root.db
views can always access the resource context, and the resource context can
always access the root, and therefore the mongo db.
class MongoRootFactory(object):
"""Root Factory with a MongoDB connection"""
def __init__(self, settings):
"""Initialize the factory by creating the mongo connection"""
self.__mongo_uri = settings['mongo.uri']
self.__mongo_db = settings['mongo.db']
#Create the MongoDB connection
self.connection = pymongo.Connection(self.__mongo_uri)
self.db = self.connection[ self.__mongo_db ]
self.fs = GridFS(self.db)
#Add the manipulator
self.db.add_son_manipulator( AutoDateManipulator() )
def __call__(self, request):
"""Return the root of the resource hierarchy"""
#Create the root resource if it doesn't exists.
if self.root == None:
self.root = self.create_root()
#Update the request object so the resource can access it.
self.root.request = request
self.root.db = self.db
self.root.fs = self.fs
return self.root
def create_root(self):
#log.debug("RootFactory: create the root object")
return Root()
The RootFactory object lives inside the application, so the connection
doesn't get created/closed for every request. I also find that the DB
connection is something that should belong to the resources, not the
request. (and you can always get it from the request using request.root.db
oO
--
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/pylons-discuss?hl=en.