Skip to content
alabid edited this page Aug 7, 2012 · 6 revisions

Models for your whirlwind application should be placed in application/models. By default, whirlwind stores data persistently in mongodb, a no-sql database. Mongodb is accessed via MongoKit. Whirlwind's model structuring is highly dependent on MongoKit, a python module that "brings structured schema and validation layer on top of the great pymongo driver".

Check out the MongoKit documentation here.

Creating a new Model

To create a new model, we advise you create a new file in application/models that's named after the model you wish to create. For example, to create a User model, create a new file user.py. The first thing you should do is import the necessary modules:

## Necessary modules
from mongokit import *
from whirlwind.db.mongo import Mongo
from tornado import options                                                       

# others I might use
import datetime                                                              
import hashlib, hmac, base64, re

Then define a Model that should be a subclass of the Document MongoKit class. But before declaring the Model class, always register the Model via the whirlwind decorator:

@Mongo.db.connection.register 
class User(Document):                                                        
   structure = {                                                            
                '_id':unicode,                                              
                'email':unicode,                                            
                'roles':list,                                               
                'password':unicode,                                         
                'created_at':datetime.datetime,                             
                'history' : {                                               
                            'last_login' : datetime.datetime,              
                            'num_logins' : long                            
                             },                                             
                'timezone':unicode,                                         
                'suspended_at':datetime.datetime,                           
                }                                                           
   use_dot_notation=True 

Check out the documentation on the Mongokit Document class here.

Interacting with the Model

Having created the model, we'd want to use it to store and retrieve information about our whirlwind application.

  • Db transactions?
  • How does the DB transactions hapen
  • How do you save stuff in the model?

Clone this wiki locally