#models/base.py from datetime import datetime from flask_sqlalchemy import SQLAlchemy as _SQLAlchemy ,BaseQuery from sqlalchemy import Column, SmallInteger, Integer from contextlib import contextmanager class SQLAlchemy(_SQLAlchemy): @contextmanager def auto_commit(self): try: yield self.session.commit() except Exception as e: db.session.rollback() raise e class Query(BaseQuery): def filter_by(self, **kwargs): if 'status' not in kwargs.keys(): kwargs['status'] = 1 return super(Query, self).filter_by(**kwargs) db = SQLAlchemy(query_class = Query) class Base(db.Model): __abstract__ = True create_time = Column('create_time',Integer) status = Column(SmallInteger,default=1) def __init__(self): self.create_time = int(datetime.now().timestamp) def set_attrs(self,attrs_dict): for key,value in attrs_dict.items(): if hasattr(self,key) and key != 'id': setattr(self,key,value) def create_datetime(self): if self.create_time: return datetime.fromtimestamp(self.create_time) else: return None
7月老师深入浅出剖析Flask核心机制,和你一起探讨Python高级编程
了解课程