[Answered ]-Django – "Find latest" of children for all rows

2👍

This should work:

from django.db.models import Max, F

Word.objects.annotate(latest=Max('attempt__when'))
            .filter(attempt__success=True,
                    attempt__when=F('latest'))

First, every Word is annotated with the date of the most recent (i.e. the Max) attempt. Then the Word objects are filtered to only include ones that have a matching Attempt where success is True and where the date matches the latest date. (F is used to represent the value of another column—or in this case, an annotation.)

Leave a comment