base.py from contextlib import contextmanager from flask_sqlalchemy import SQLAlchemy as _SQLAlchemy from sqlalchemy import Column, Integer, SmallInteger class SQLAlchemy(_SQLAlchemy): @contextmanager def auto_commit(self): try: yield self.session.commit() except Exception as e: # rollback(回滚) db.session.rollback() raise e db = SQLAlchemy() class Base(db.Model): __abstract__ = True create_time = Column('create_time', Integer) status = Column(SmallInteger, default=1) def set_attrs(self, attrs): for key, value in attrs.items(): if hasattr(self, key) and key != 'id': setattr(self, key, value)
7月老师深入浅出剖析Flask核心机制,和你一起探讨Python高级编程
了解课程