Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用django聚合作为筛选子句_Django_Django Models_Python - Fatal编程技术网

使用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()正是我想要的。谢谢你的帮助!