使用django聚合作为筛选子句
有更好的方法吗使用django聚合作为筛选子句,django,django-models,python,Django,Django Models,Python,有更好的方法吗 # 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
# 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'])
必须有一种更优雅的方法来做到这一点吗?使用最新()方法来获取“最大”日期是否符合您的需要
可能有兴趣。latest()正是我想要的。谢谢你的帮助!