Flask 封装save delete 方法

2020-12-08  本文已影响0人  垃圾桶边的狗

class BaseModel(db.Model):
    __abstract__ = True
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)

    def save(self):
        try:
            db.session.add(self)
            db.session.commit()
        except Exception as e:
            print(e)
            return False
        else:
            return True

    @staticmethod
    def save_all(*args):
        try:
            db.session.add_all(*args)
            db.session.commit()
        except Exception as e:
            print(e)
            return False
        else:
            return True


    def delete(self):
        try:
            db.session.delete(self)
            db.session.commit()
        except Exception as e:
            print(e)
            return False
        else:
            return True
上一篇下一篇

猜你喜欢

热点阅读