Leveraging the power of NoSQL

To show the unique power of NoSQL, let's add a feature that would be possible with SQLAlchemy, but which would be much more difficult: different post types, each with their own custom bodies. This will be much like the functionality of the popular blog platform Tumblr.

To begin, allow your post type to act as a parent class and remove the text field from the Post class, as not all posts will have text on them. This is shown in the following code:

class Post(mongo.Document): title = mongo.StringField(required=True) publish_date = mongo.DateTimeField(default=datetime.datetime.now()) user = mongo.ReferenceField(Userm) comments = mongo.ListField( mongo.EmbeddedDocumentField(Commentm) ) tags = mongo.ListField(mongo.StringField()) ...

Get Mastering Flask Web Development - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.