Пример #1
0
 def save(self):
     """
     Save this instance to the database. Validation is performed first.
     """
     d = self.validate()
     self.get_collection().save(d)
     if not self._id:
         self._id = d['_id']
         registry.add_model_instance(self)
         self.was_created()
     else:
         self.was_modified()
Пример #2
0
 def __init__(self, **kwargs):
     """
     Create a new instance of a model optionally setting any of its fields::
     
         class Swallow(Model):
             is_laden = Bool()
             speed = TypeOf(float)
             direction = Enum('N', 'E', 'S', 'W')
         
         # Create a model and set all its fields
         s = Swallow(is_laden=True, speed=23.7, direction='E')
         
         # Create a model and then set some fields
         t = Swallow()
         t.is_laden = False
         
         # Validation doesn't occur until save
         t.save()
         # raises InvalidGroupError since this instance is missing two fields
         
         t.speed = 5
         t.direction = 'S'
         t.save() # ok, we are in the db now
         
     """
     self._reference_fields = {}
     for k,v in self.__class__.__dict__.items():
         if isinstance(v, Validator):
             setattr(self, k, kwargs.get(k))
         elif isinstance(v, Relationship):
             self._reference_fields[k] = kwargs.get(k)
     setattr(self, '_id', kwargs.get('_id'))
     self.remove = self._remove
     
     if self._id:
         registry.add_model_instance(self)