Smarter Python Data Models
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.


Smarter Python Data Models

  • aware of data store. If SQL, table/column does not exist it creates it. Can also store unknown attributes in json columns. If NoSQL, then it is smart enough to save as is.
  • goes to and from json seemlessly
  • Can be created/updated from dict or attributes
  • validation... is valid
  • Aware of changed fields and previous values
  • Based on Models from Backbone js
  • how to model relationships?
class MagicModel:
  changed = {}
  attributes = {}
  defaults = {}
  validation_errors = []
  id_attribute = 'id'

  # Create a new model with the specified attributes. 
  def __init( self, attributes, options )__:
    # apply defaults 
    # apply attributes
    # set changed to {}
  # Return a copy of the model's `attributes` dict
  def to_json( self ):

  # sync to datastore
  def sync( self ):
  # Get the value of an attribute.
  def get( self, attr ):
  # Returns `true` if the attribute contains a value that is not null
  # or undefined.
  def has( self, attr ):
  # Set a hash of model attributes on the object
  def set( self, key, val, options ):
  # Remove an attribute from the model  
  def unset( self, attr ):
  # Clear all attributes on the model  
  def clear( self ):
  # Determine if the model has changed since the last `"change"` event.
  # If you specify an attribute name, determine if that attribute has changed.    
  def has_changed( self, attr = None ):

  # Return an object containing all the attributes that have changed, or
  # false if there are no changed attributes. 
  # You can also pass an attributes object to diff against the model,
  # determining if there *would be* a change.
  def changedAttributes( self, diff = None ):

  # Get the previous value of an attribute, recorded at the time the last
  # `"change"` event was fired.
  def previous( self, attr = None ):

  # Get all of the attributes of the model at the time of the previous
  # `"change"` event.
  def previous_attributes( self ):
  # Fetch the model from the server. If the server's representation of the
  # model differs from its current attributes, they will be overridden,    
  def fetch( self ): 

  # Set a hash of model attributes, and sync the model to the server.
  # If the server returns an attributes hash that differs, the model's
  # state will be `set` again.
  def save( self ):

  # Destroy this model on the server if it was already persisted.
  def destroy( self ):

  # **parse** converts a response into the hash of attributes to be `set` on
  # the model. The default implementation is just to pass the response along.
  def parse( self, json ):

  # Create a new model with identical attributes to this one.
  def clone( self ): 

  # A model is new if it has never been saved to the server, and lacks an id.
  def isNew( self ): 

  # Run validation against the next complete set of model attributes,
  # returning `true` if all is well. Otherwise, fire an `"invalid"` event.  
  def validate( self, attrs ):  

  # Check if the model is currently in a valid state
  def is_valid( self ):