Sqlalchemy复用子查询

3

我希望能够重复使用我的子查询多次。

第一次计数

 devices = db.session.query(subquery).count()

子查询的使用

 (Device, User).\
                    filter(Device.e=='E').\
                 filter(Device.id==User.id

然后使用limit对结果进行分页。我想重复使用我的筛选器

(subquery).paginate(page=offset, per_page=limit)

不要重复,因为我使用了很多过滤器,而且过滤器可能会改变。

1个回答

1
答案其实非常简单。
def getquery(self):
  return db.session.query(subquery)

计数循环

count = self.getquery().count()

对于限制

devices = self.getquery().limit(xxxx)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接