From c50d5e79218f96259485a7ff43ae4ad04ff31b2a Mon Sep 17 00:00:00 2001 From: sunshine <53550304+resonates@users.noreply.github.com> Date: Tue, 12 Apr 2022 16:55:27 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E5=86=99sqlalchemy=E7=9A=84query,?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=BA=8F=E5=88=97=E5=8C=96=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- applications/extensions/init_sqlalchemy.py | 28 ++++++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/applications/extensions/init_sqlalchemy.py b/applications/extensions/init_sqlalchemy.py index ed9fdc0..13e68d4 100644 --- a/applications/extensions/init_sqlalchemy.py +++ b/applications/extensions/init_sqlalchemy.py @@ -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()