美文网首页
Flask 封装save delete 方法

Flask 封装save delete 方法

作者: 垃圾桶边的狗 | 来源:发表于2020-12-08 14:41 被阅读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

相关文章

网友评论

      本文标题:Flask 封装save delete 方法

      本文链接:https://www.haomeiwen.com/subject/tyofgktx.html