2024-05-19 10:28:14 发布
网友
我用SQLalchemy查询数据库。我可以选择所有多行/对象,方法如下:
def selectAllObjects(): objects = session.query(Object).all()
但是,我想构建一个函数来检查是否有任何给定的约束,如果有则包含这些约束。所以我就按照以下思路思考:
如您所见,当属性数量增加时,这会变得非常糟糕。Python的方法是什么?在
Query对象可从docs链接:
Query
Query is produced in terms of a given Session, using the query() method:q = session.query(SomeMappedClass)
Query is produced in terms of a given Session, using the query() method:
q = session.query(SomeMappedClass)
你可以这样写:
def selectAllObjects(attribute1="default", attribute2="default"): query = session.query(Object) if attribute1 != "default" and attribute2 != "default": query = query.filter_by( attribute1=attribute1, attribute2=attribute2, ) elif attribute1 != "default": query = query.filter_by(attribute1=attribute1) return query.all()
Query
对象可从docs链接:你可以这样写:
相关问题 更多 >
编程相关推荐