[Answered ]-Selecting and sorting on unique foreign key with Django

2👍

This is not quite what you asked, but gives you the list of user IDs and their associated max speeds:

from django.db.models import Max
Speed.objects.values_list('user').annotate(Max('speed'))

This might be more useful: a list of User objects with their max speeds:

User.objects.all().annotate(Max('speed__speed'))

Edit after comment Well, you don’t show where the time is coming from, but assuming that it’s another field on the Speed model, it’s still fairly simple:

Speed.objects.filter(time__gte=fiveminutesago).values_list('user').annotate(Max('speed'))

See the documentation on ordering of annotate and filter clauses.

Leave a comment