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.
精彩评论