Tag: mongoengine

How to integrate mongoengine in pyramid

Programming

For a project in the university i had to build a webapp, since it was a edu-based project and i wanted to try something which diverges from the classic Apache+django+mysql approach and i built a Cherokee+pyramid+mongodb applciation. In this article ill explain how to use mongoengine, a state-of-the-art django like ORM for mongodb in your pyramid application.

First of all i think that pyramid has the best and liter interface i’ve ever seen. This will let u integrate mongodb with very small work. If u ever used mongoengine, u just need to call:


from mongoengine import connect

connect('database')

So we need to call it in our application. In pyramid all the application is initialized in the __init__.py file which has this content:

from pyramid.config import Configurator
from myproject.resources import Root

def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    config = Configurator(root_factory=Root, settings=settings)
    config.add_view('myproject.views.my_view',
                    context='myproject.resources.Root',
                    renderer='myproject:templates/mytemplate.pt')
    config.add_static_view('static', 'myproject:static')
    return config.make_wsgi_app()

You have to change it to be like:

from pyramid.config import Configurator
from myproject.resources import Root

def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    config = Configurator(root_factory=Root, settings=settings)
    config.add_view('myproject.views.my_view',
                    context='myproject.resources.Root',
                    renderer='myproject:templates/mytemplate.pt')
    config.add_static_view('static', 'myproject:static')
    #this will make mongoengine working
    connect(settings['db_name'])
    #this will make all the @view_config in the view working
    config.scan()
    return config.make_wsgi_app()

Of course u have to define your db_name in production.ini or development.ini or both in a way which looks like this:

[app:milo_app]
use = egg:milo_app
reload_templates = true
debug_authorization = false
debug_notfound = false
debug_routematch = false
debug_templates = true
default_locale_name = en
db_name = milo

That’s it, u can now use mongoengine in the resources.py and in any other file u want to create like this:

from mongoengine import *
class User(Document):
	email = StringField(required=True)
	first_name = StringField()
	last_name = StringField()
	password = StringField()
	cwid = IntField()

class Comment(EmbeddedDocument):
	autor = ReferenceField(User)
	content = StringField()

class Genre(EmbeddedDocument):
	name = StringField()

class Movie(Document):
	__name__ = 'Movie'
	__parent__ = Root

	#movie is identified by title and year
	title = StringField(required=True)
	date = DateTimeField()
	description = StringField()
	trailer = URLField()
	poster = StringField()
	image = StringField()
	genre = ListField(StringField())
	comments = ListField(EmbeddedDocumentField(Comment))

	def __str__(self):
		return 'Movie(%s, %s, %s, %s, %s)' % (self.title, self.date, self.poster, self.image, self.trailer)

then in any view u can do:

Movie.objects()
User.objects()

To retrieve all the movies and all the users (in my case)
I hope this helps!

%d bloggers like this: