This is a shortcut to adding the object to a session (if
needed) and giving a commit on the session.
In reality, calling code should probably utilize the
session on its own and call commit in an appropiate place.
-from sqlalchemy.orm import scoped_session, sessionmaker
+from sqlalchemy.orm import scoped_session, sessionmaker, object_session
Session = scoped_session(sessionmaker())
def get(self, key):
return getattr(self, key)
+
+ def save(self, validate = True):
+ assert validate
+ sess = object_session(self)
+ if sess is None:
+ sess = Session()
+ sess.add(self)
+ sess.commit()