简体   繁体   English

我如何通过查询来命令,以便所有列的列等于true?

[英]How do i order_by a query so that all rows with a column equal to true go at first?

So i have a model : 所以我有一个模型:

id = db.Column(db.Integer, primary_key=True) 
is_urgent = db.Column(db.Boolean, default=False)
creation_time = db.Column(db.DateTime, index=True, default=datetime.utcnow)

and i want to create a ordered list by a query so that all orders that are urgent go first and then all that arent urgent. 并且我想通过查询创建一个有序列表,以便所有紧急的订单首先出现,然后是所有不紧急的。 And those two gropus are also ordered by creation time, so the first order of the query list would be the oldest urgent order and the most recent would be the not-urgent newest one, 并且这两个gropus也按创建时间排序,因此查询列表的第一个顺序是最旧的紧急订单,最新的紧急订单是最近的紧急订单,

can it be done with pure sqlalchemy ? 可以用纯粹的sqlalchemy来完成吗? or should i create a for loop after a simpler query ? 或者我应该在更简单的查询后创建一个for循环?

Query results can be ordered by using the order_by method on your query. 可以使用查询中的order_by方法对查询结果进行排序。 This takes multiple arguments and your query will be sorted by each of these in turn. 这需要多个参数,您的查询将依次按每个参数进行排序。 The desc method on each of the columns supplied to order_by can be used to control the direction of the sort. 提供给order_by每个列的desc方法可用于控制排序的方向。 The following should work for you. 以下内容适合您。

session.query(MyModel).order_by(MyModel.is_urgent.desc(), MyModel.creation_time)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Flask-SQLAlchemy 中执行多个“order_by”? - How can I do multiple "order_by" in Flask-SQLAlchemy? 带有 order_by 的 Flask SQLAlchemy 查询返回错误没有这样的列 - Flask SQLAlchemy query with order_by returns error no such column SQLAlchemy:如何在关系字段上排序查询结果(order_by)? - SQLAlchemy: How to order query results (order_by) on a relationship's field? 我可以在查询调用中动态更改order_by属性吗? - Can I dynamically change order_by attributes in my query call? 如何按关系 model 中的列的 hybrid_property 排序? - How to order_by hybrid_property which is a column in relationship model? 如何在一个列上使用group_by,在另一列上使用order_by,在第三列上使用过滤器? - How can I have a group_by on one column, order_by on another column, and a filter on the 3rd different column? 具有 int 值的 SQLAlchemy order_by 字符串列 - SQLAlchemy order_by string column with int values SQLAlchemy:如何对关系字段上的查询结果(order_by)进行排序? [重新] - SQLAlchemy: How to order query results (order_by) on a relationship's field? [revisited] 如何在backref(..,order_by = ..)中的mixin类中使用SQLAlchemy的列名? - How to use a column name from mixin class inside backref(.., order_by=..) with SQLAlchemy? 如何指定多个“where”或“order_by”条件? - How to specify multiple "where" or "order_by" conditions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM