SQLAlchemy更新或插入函数

时间:2011-09-01 21:28:27

标签: python orm sqlalchemy

一般使用SQLAlchemy和ORM的新手。 我有一个函数,我想调用一个对象,并取决于是否已将现有的id传递给函数INSERT或更新数据库。

def storeFoo(self,session,FooObj=None,Fooid=None):
    if Fooid:
        Foo = session.query(ConfFoo).\
                    filter(ConfFoo.id == self.id).first()
        #Update Values
        Foo.name = self.name
        Foo.startdate = self.startdate
        Foo.enddate = self.enddate
        Foo.timeperiod = self.timeperiod
        Foo.users = self.users
        Foo.keywords = self.keywords
        Foo.type = self.type
        Foo.perms = self.perms
        session.add(Foo)

    else:
        Foo = ConfFoo(name = FooObj.name, startdate=FooObj.startdate,
                            enddate = FooObj.enddate, timeperiod = FooObj.timeperiod,
                            users = FooObj.users, keywords = FooObj.keywords,
                            type = FooObj.type, perms = FooObj.perms)
        session.add(Foo)

0 个答案:

没有答案