diff --git a/chassis/db_model.py b/chassis/db_model.py index 2cda20f..a03294a 100644 --- a/chassis/db_model.py +++ b/chassis/db_model.py @@ -1,4 +1,5 @@ from sqlalchemy import Column, DateTime, Boolean, func +from sqlalchemy.orm import Query from sqlalchemy.ext.declarative import declarative_base from datetime import datetime @@ -23,6 +24,13 @@ class BaseCrud(BaseModel): return session.query(cls).filter().filter_by(**kwargs).first() return session.query(cls).filter().filter_by(**kwargs).offset(start).limit(count).all() + @classmethod + def filtered_query(cls, session, filter_args=None, filter_kwargs=None) -> Query: + if filter_args: + return session.query(cls).filter(*filter_args).filter_by(**filter_kwargs) + else: + return session.query(cls).filter().filter_by(**filter_kwargs) + # 增 @classmethod def create(cls, session, **kwargs): @@ -45,10 +53,10 @@ class BaseCrud(BaseModel): return self -# 提供软删除,及创建时间,更新时间信息的crud model - - class InfoCrud(BaseModel): + """ + 提供软删除,及创建时间,更新时间信息的crud model + """ __abstract__ = True @@ -80,6 +88,13 @@ class InfoCrud(BaseModel): return session.query(cls).filter().filter_by(**kwargs).first() return session.query(cls).filter().filter_by(**kwargs).offset(start).limit(count).all() + @classmethod + def filtered_query(cls, session, filter_args=None, filter_kwargs=None) -> Query: + if filter_args: + return session.query(cls).filter(*filter_args).filter_by(**filter_kwargs) + else: + return session.query(cls).filter().filter_by(**filter_kwargs) + # 增 @classmethod def create(cls, session, **kwargs):