Skip to content

Instantly share code, notes, and snippets.

@dmazzer
Created October 28, 2016 18:54
Show Gist options
  • Save dmazzer/4c52818f0d955297e40d41ef824b0e54 to your computer and use it in GitHub Desktop.
Save dmazzer/4c52818f0d955297e40d41ef824b0e54 to your computer and use it in GitHub Desktop.
# http://docs.mongoengine.org/tutorial.html
from mongoengine import *
db = connect('tumblelog')
db.drop_database('tumblelog')
##################
class User(Document):
email = StringField(required=True)
first_name = StringField(max_length=50)
last_name = StringField(max_length=50)
##################
class Comment(EmbeddedDocument):
content = StringField()
name = StringField(max_length=120)
##################
class Post(Document):
title = StringField(max_length=120, required=True)
author = ReferenceField(User)
tags = ListField(StringField(max_length=30))
comments = ListField(EmbeddedDocumentField(Comment))
meta = {'allow_inheritance': True}
class TextPost(Post):
content = StringField()
class ImagePost(Post):
image_path = StringField()
class LinkPost(Post):
link_url = StringField()
##################
ross = User(email='[email protected]', first_name='Ross', last_name='Lawley').save()
ross['last_name'] = 'changed'
print(ross.id)
ross.save()
for user in User.objects:
print (user.first_name)
post1 = TextPost(title='Fun with MongoEngine', author=ross)
post1.content = 'Took a look at MongoEngine today, looks pretty cool.'
post1.tags = ['mongodb', 'mongoengine']
post1.save()
post2 = LinkPost(title='MongoEngine Documentation', author=ross)
post2.link_url = 'http://docs.mongoengine.com/'
post2.tags = ['mongoengine']
post2.save()
for post in Post.objects(tags='mongodb'):
print (post.title)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment