重写sqlalchemy的query,增加序列化方法
This commit is contained in:
@@ -55,16 +55,34 @@ class Query(BaseQuery):
|
||||
def logic_all(self):
|
||||
return self.filter_by(delete_at=None).all()
|
||||
|
||||
def all_json(self, schema: Marshmallow().Schema):
|
||||
return schema(many=True).dump(self.all())
|
||||
|
||||
def layui_paginate(self):
|
||||
"""
|
||||
layui表格分页
|
||||
page
|
||||
limit
|
||||
"""
|
||||
return self.paginate(page=request.args.get('page', type=int),
|
||||
per_page=request.args.get('limit', type=int),
|
||||
error_out=False)
|
||||
|
||||
def layui_paginate_json(self, schema: Marshmallow().Schema):
|
||||
"""
|
||||
返回dict
|
||||
"""
|
||||
_res = self.paginate(
|
||||
page=request.args.get('page', type=int),
|
||||
per_page=request.args.get('limit', type=int),
|
||||
error_out=False
|
||||
)
|
||||
return schema(many=True).dump(_res.items), _res.total, _res.page, _res.per_page
|
||||
|
||||
def layui_paginate_db_json(self):
|
||||
"""
|
||||
db.query(A.name).layui_paginate_db_json()
|
||||
"""
|
||||
_res = self.paginate(page=request.args.get('page', type=int),
|
||||
per_page=request.args.get('limit', type=int),
|
||||
error_out=False)
|
||||
return [dict(i) for i in _res.items], _res.total
|
||||
|
||||
|
||||
db = SQLAlchemy(query_class=Query)
|
||||
ma = Marshmallow()
|
||||
|
||||
Reference in New Issue
Block a user