Add a .save method on the sql db objects

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.
This commit is contained in:
Elrond 2011-12-25 19:09:23 +01:00
parent 03c2286232
commit 9f264942d8

View File

@ -1,4 +1,4 @@
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.orm import scoped_session, sessionmaker, object_session
Session = scoped_session(sessionmaker())
@ -28,3 +28,11 @@ class GMGTableBase(object):
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()