使用django聚合作为过滤子句

Using django aggregates as filter clauses

有更好的方法吗?

1
2
3
4
5
6
7
8
# Get an instance of the class I am interested in. It has a manytomany relation
instance = Class.objects.get(pk=1)

# Get the highest start date out of all the objects in the manytomany set
max_date = instance.related_set.aggregate(Max("start_date"))

# Return the object with the highest start date in the manytomany set
query_result = instance.related_set.filter(start_date=max_date['start_date__max'])

一定有更优雅的方式来做这个对吗?


使用最新的()方法获取"max"日期是否适合您的需要?

django-query-using-order-by-and-latest

可能感兴趣。