开发者

Using django aggregates as filter clauses

开发者 https://www.devze.com 2023-02-16 09:23 出处:网络
Is there a better way to do this? # Get an instance of the class I am interested in. It has a manytomany relation

Is there a better way to do this?

# 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_d开发者_Go百科ate['start_date__max'])

There must be a more elegant way to do this right?


Would using the latest() method for getting the "max" date suit your needs?

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

might be of interest.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号