user = request.user
users = User.objects.filter(Q(r__sender=user) | Q(s__recipient=user)).distinct(
    ).extra(select={'last_message_time':
    'select MAX(sent_at) from appname_usercomment where (recipient_id=auth_user.id and sender_id=%s) or (recipient_id=%s and sender_id=auth_user.id)'
    }, select_params=(user.id, user.id)).extra(order_by=['-last_message_time']
    ).extra(select={'message':
    'select comment from appname_usercomment where (sent_at=(select MAX(sent_at) from appname_usercomment where (recipient_id=auth_user.id and sender_id=%s) or (recipient_id=%s and sender_id=auth_user.id)) and ((recipient_id=auth_user.id and sender_id=%s) or (recipient_id=%s and sender_id=auth_user.id)))'
    }, select_params=(user.id, user.id, user.id, user.id))
How to show the last message of each user to user conversations to keep a chat history?