[Solved]-Django ORM โ€“ Get latest record for group

18๐Ÿ‘

โœ…

Put your values clause before the annotate.

From the aggregation docs:

If the values() clause precedes the annotate(), the annotation will be computed using the grouping described by the values() clause.

However, if the annotate() clause precedes the values() clause, the annotations will be generated over the entire query set. In this case, the values() clause only constrains the fields that are generated on output.

So this should do it:

Meetup.objects.values('language').annotate(latest_date=Max('date'))
๐Ÿ‘คPeter DeGlopper

Leave a comment