如何使查询集成为order_by,然后再使另一个订单保持第一个结果呢?

时间:2019-06-12 20:26:31

标签: django django-models django-views

我需要用两列对queryset结果进行排序。 首先,我需要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

1 个答案:

答案 0 :(得分:0)

您需要先截断到* installing *source* package ‘cairoDevice’ ... ** package ‘cairoDevice’ successfully unpacked and MD5 sums checked checking for pkg-config... no ERROR: Cannot find pkg-config. ERROR: configuration failed for package ‘cairoDevice’ * removing ‘/Users/danielanzola/anaconda3/lib/R/library/cairoDevice’ Warning in install.packages : installation of package ‘cairoDevice’ had non-zero exit status * installing *source* package ‘Matrix’ ... ** package ‘Matrix’ successfully unpacked and MD5 sums checked ** libs x86_64-apple-darwin13.4.0-clang -I"/Users/danielanzola/anaconda3/lib/R/include" -DNDEBUG -DNTIMER -I./SuiteSparse_config -D_FORTIFY_SOURCE=2 -mmacosx-version-min=10.9 -I/Users/danielanzola/anaconda3/include -fPIC -march=core2 -mtune=haswell -mssse3 -ftree-vectorize -fPIC -fPIE -fstack-protector-strong -O2 -pipe -I/Users/danielanzola/anaconda3/include -fdebug-prefix-map==/usr/local/src/conda/- -fdebug-prefix-map==/usr/local/src/conda-prefix -c CHMfactor.c -o CHMfactor.o /bin/sh: x86_64-apple-darwin13.4.0-clang: command not found make: *** [CHMfactor.o] Error 127 ERROR: compilation failed for package ‘Matrix’ * removing ‘/Users/danielanzola/anaconda3/lib/R/library/Matrix’ Warning in install.packages : installation of package ‘Matrix’ had non-zero exit status * installing *source* package ‘RSQLite’ ... ** package ‘RSQLite’ successfully unpacked and MD5 sums checked ** libs x86_64-apple-darwin13.4.0-clang++ -I"/Users/danielanzola/anaconda3/lib/R/include" -DNDEBUG -I. -DRSQLITE_USE_BUNDLED_SQLITE -DSQLITE_ENABLE_RTREE -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_FTS5 -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_STAT4 -DSQLITE_SOUNDEX -I"/Users/danielanzola/anaconda3/lib/R/library/BH/include" -I"/Users/danielanzola/anaconda3/lib/R/library/plogr/include" -I"/Users/danielanzola/anaconda3/lib/R/library/Rcpp/include" -D_FORTIFY_SOURCE=2 -mmacosx-version-min=10.9 -I/Users/danielanzola/anaconda3/include -fPIC -march=core2 -mtune=haswell -mssse3 -ftree-vectorize -fPIC -fPIE -fstack-protector-strong -O2 -pipe -stdlib=libc++ -fvisibility-inlines-hidden -fmessage-length=0 -I/Users/danielanzola/anaconda3/include -fdebug-prefix-map==/usr/local/src/conda/- -fdebug-prefix-map==/usr/local/src/conda-prefix -c DbColumn.cpp -o DbColumn.o /bin/sh: x86_64-apple-darwin13.4.0-clang++: command not found make: *** [DbColumn.o] Error 127 ERROR: compilation failed for package ‘RSQLite’ * removing ‘/Users/danielanzola/anaconda3/lib/R/library/RSQLite’ Warning in install.packages : installation of package ‘RSQLite’ had non-zero exit status * installing *source* package ‘RGtk2’ ... ** package ‘RGtk2’ successfully unpacked and MD5 sums checked checking for pkg-config... no checking for INTROSPECTION... no checking for GTK... no configure: error: GTK version 2.8.0 required ERROR: configuration failed for package ‘RGtk2’ * removing ‘/Users/danielanzola/anaconda3/lib/R/library/RGtk2’ Warning in install.packages : installation of package ‘RGtk2’ had non-zero exit status ERROR: dependencies ‘RGtk2’, ‘cairoDevice’ are not available for package ‘gWidgetsRGtk2’ * removing ‘/Users/danielanzola/anaconda3/lib/R/library/gWidgetsRGtk2’ Warning in install.packages : installation of package ‘gWidgetsRGtk2’ had non-zero exit status ERROR: dependency ‘Matrix’ is not available for package ‘igraph’ * removing ‘/Users/danielanzola/anaconda3/lib/R/library/igraph’ Warning in install.packages : installation of package ‘igraph’ had non-zero exit status ERROR: dependencies ‘RSQLite’, ‘gWidgetsRGtk2’, ‘RGtk2’, ‘igraph’ are not available for package ‘RQDA’ * removing ‘/Users/danielanzola/anaconda3/lib/R/library/RQDA’ Warning in install.packages : installation of package ‘RQDA’ had non-zero exit status 的日期。我们可以使用TruncDate [Django-doc]来做到这一点:

DateTimeField

源自 this 查询集的from django.db.models.functions import TruncDate Measurements.objects.annotate( date_date=TruncDate('date') ).order_by('-date_date', '-status')对象将具有一个额外的属性Measurements,该属性会截断.date_date直到日期(因此时间为然后date