【发布时间】:2019-10-27 10:07:52
【问题描述】:
我需要对包含两列的查询集结果进行排序。 首先,我需要 order_by 'date'。对查询进行排序后,我需要对结果进行排序,并使用 order_by 'status' 保持第一顺序。
例子:
date status
2019-06-10 15:23:39 20
2019-06-12 15:37:31 20
2019-06-10 15:18:53 10
2019-06-12 15:38:01 10
2019-06-12 15:47:09 10
2019-06-12 15:36:30 0
2019-06-10 16:35:11 0
我需要以下结果:
date status
2019-06-12 15:37:31 20
2019-06-12 15:47:09 10
2019-06-12 15:38:01 10
2019-06-12 15:36:30 0
2019-06-10 15:23:39 20
2019-06-10 15:18:53 10
2019-06-10 16:35:11 0
关注 dkango 文档https://docs.djangoproject.com/en/2.0/ref/models/querysets/#order-by 我正在做一个 order_by 传递两个字段,如 order_by('-date', '-status')
我正在尝试以下代码
Measurements.objects.filter(patient=230).order_by('-date', '-status')
我的结果是:
date status
2019-06-12 15:47:09 10
2019-06-12 15:38:01 10
2019-06-12 15:37:31 20
2019-06-12 15:36:30 0
2019-06-10 16:35:11 0
2019-06-10 15:23:39 20
2019-06-10 15:18:53 10
我需要以下结果:
date status
2019-06-12 15:37:31 20
2019-06-12 15:47:09 10
2019-06-12 15:38:01 10
2019-06-12 15:36:30 0
2019-06-10 15:23:39 20
2019-06-10 15:18:53 10
2019-06-10 16:35:11 0
【问题讨论】:
标签: django django-models django-views