栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

SQLAlchemy-编写用于子计数的混合方法

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SQLAlchemy-编写用于子计数的混合方法

以下代码显示了所有内容。

class Parent(base):    __tablename__ = 'parents'    # ...    @hybrid_property    def child_count(self):        #return len(self.children)   # @note: use when non-dynamic relationship        return self.children.count()# @note: use when dynamic relationship    @child_count.expression    def child_count(cls):        return (select([func.count(Child.child_id)]).     where(Child.parent_id == cls.parent_id).     label("child_count")     )    @hybrid_method    def child_count_ex(self, stime, etime):        return len([_child for _child in self.children if stime <= _child.time <= etime ])    @child_count_ex.expression    def child_count_ex(cls, stime, etime):        return (select([func.count(Child.child_id)]).     where(Child.parent_id == cls.parent_id).     where(Child.time >= stime).     where(Child.time <= etime).     label("child_count")     )# usage of expressions:stime, etime = datetime.datetime(2012, 1, 1), datetime.datetime(2012, 1, 31)qry = session.query(Parent)#qry = qry.filter(Parent.child_count > 2)qry = qry.filter(Parent.child_count_ex(stime, etime) > 0)


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/611019.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号