[Answered ]-How to get latest value of a field on a related model in Django?

2👍

threads = Thread.objects.filter(
            Q(user1=request.user) | Q(user2=request.user)
          ).annotate(
            latest_msg=Max('message__modified')
          ).values(
            'message__text',
            'fields you want to fetch'
          ).order_by('-latest_msg')[:1]

Leave a comment