【发布时间】:2021-05-21 16:19:06
【问题描述】:
如何将 order_by() 与 distinct() 结合使用?
我有多个具有不同 end_time 的相同 run_id 并尝试过滤不同的 run_id 并按 end_time 排序
data = list(table_name.objects.filter(experience=experience)\
.values('run_id', 'end_time').distinct('run_id').order_by('-end_time'))
以下是错误:-
django.db.utils.ProgrammingError: SELECT DISTINCT ON expressions must match initial ORDER BY expressions
但是,following ORM works fine without order_by 但我想要最新的 end_time
data = list(table_name.objects.filter(experience=experience).values('run_id', 'end_time').distinct('run_id'))
【问题讨论】:
标签: django django-models sql-order-by django-filters django-aggregation