如何在 django 中按 max(...) 进行分组和排序
How to make a Group by and order by max(...) in django
我想获取 table 出版物中的最后一个类别。
这是正确的 SQL 代码:
select id_categoria_id, max(update_time) from showgroups_publicacion group by id_categoria_id order by max(update_time) desc
1 | "2015-05-04 18:25:19-04"
5 | "2015-05-04 18:25:15-04"
0 | "2015-05-04 18:24:35-04"
2 | "2015-05-04 18:12:13-04"
在 django 中我有这个:
cat_pub = Publicacion.objects.all().values('id_categoria').annotate(Max('update_time'))
cat_pub.query:
SELECT showgroups_publicacion.id_categoria_id, MAX(showgroups_publicacion.update_time) AS update_time__max FROM showgroups_publicacion GROUP BY showgroups_publicacion.id_categoria_id
0 | "2015-05-04 18:24:35-04"
1 | "2015-05-04 18:25:19-04"
2 | "2015-05-04 18:12:13-04"
5 | "2015-05-04 18:25:15-04"
我想在 django 中按 max 排序,我该如何解决?
提前致谢。
cat_pub = Publicacion.objects.all().values('id_categoria').annotate(max_update_time=Max('update_time')).order_by('-max_update_time')
我想获取 table 出版物中的最后一个类别。
这是正确的 SQL 代码:
select id_categoria_id, max(update_time) from showgroups_publicacion group by id_categoria_id order by max(update_time) desc
1 | "2015-05-04 18:25:19-04"
5 | "2015-05-04 18:25:15-04"
0 | "2015-05-04 18:24:35-04"
2 | "2015-05-04 18:12:13-04"
在 django 中我有这个:
cat_pub = Publicacion.objects.all().values('id_categoria').annotate(Max('update_time'))
cat_pub.query:
SELECT showgroups_publicacion.id_categoria_id, MAX(showgroups_publicacion.update_time) AS update_time__max FROM showgroups_publicacion GROUP BY showgroups_publicacion.id_categoria_id
0 | "2015-05-04 18:24:35-04"
1 | "2015-05-04 18:25:19-04"
2 | "2015-05-04 18:12:13-04"
5 | "2015-05-04 18:25:15-04"
我想在 django 中按 max 排序,我该如何解决?
提前致谢。
cat_pub = Publicacion.objects.all().values('id_categoria').annotate(max_update_time=Max('update_time')).order_by('-max_update_time')