Can Django ORM do an ORDER BY on a specific value of a column?(Django ORM 可以对列的特定值执行 ORDER BY 吗?)
问题描述
我有一张包含以下列的票"表
I have a table 'tickets' with the following columns
- id - 主键 - 自增
- 标题 - varchar(256)
- status - smallint(6) - 可以是 1 到 5 之间的任何值,由 Django 处理
当我执行 SELECT *
我希望 status = 4
位于顶部的行时,其他记录将跟随它们.可以通过以下查询来实现:
When I'll do a SELECT *
I want the rows with status = 4
at the top, the other records will follow them. It can be achieved by the following query:
select * from tickets order by status=4 DESC
这个查询可以通过 Django ORM 执行吗?哪些参数应该传递给 QuerySet.order_by()
方法?
Can this query be executed through Django ORM? What parameters should be passed to the QuerySet.order_by()
method?
推荐答案
q = Ticket.objects.extra(select={'is_top': "status = 4"})
q = q.extra(order_by = ['-is_top'])
这篇关于Django ORM 可以对列的特定值执行 ORDER BY 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Django ORM 可以对列的特定值执行 ORDER BY 吗?


基础教程推荐
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- 带更新的 sqlite CTE 2022-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01