如何向用户对话显示每个用户的最后一条消息以保留聊天记录? [英] How to show the last message of each user to user conversations to keep a chat history?

查看:23
本文介绍了如何向用户对话显示每个用户的最后一条消息以保留聊天记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个私人用户到用户聊天,为了与某人聊天,连接的用户必须在他自己的网址上输入他想与之交谈的用户的用户名.

I'm creating a private user to user chat, in order to chat with someone the connected user has to type the username of the user with whom he wants to talk to on his own url.

既然这个系统已经建立,我想保留一个聊天记录,以便以后我可以发送聊天通知.为此,我需要获取每个对话的最后一条消息,并将其显示在已连接用户自己的聊天配置文件中.

Now that this system is already built, I want to keep a chat history so that later on I can send notification of chat. To do that I need to get the last message of each conversations and I want to show it on the connected user's own chat profile.

如下图:

模型 userComment 字段是:recipientsendercomment、<代码>sent_at

views.py:

def inbox(request, username):
    username = User.objects.get(username=username)
    connected_user = request.user

    if username == connected_user:

        #I'm having the issue on this line
        users = userComment.objects.filter(Q(client=request.user) | Q(worker=request.user)).order_by(?) 

    else:
        users = userComment.objects.filter(Q(Q(client=request.user) & Q(worker=username)) | Q(Q(client=username) & Q(worker=request.user))).order_by('sent_at')

models.py

class userComment(models.Model):
    client = models.ForeignKey(User, related_name="client")
    worker = models.ForeignKey(User, blank=True, null=True, related_name="worker")
    sent_at = models.DateTimeField(auto_now_add=True)
    comment = models.TextField(max_length=255, null=True)

    def __str__(self):
        return str(self.client)

问题:如何过滤和排序我的视图?

Question : How can I filter and order my view to do so ?

推荐答案

首先在你的 userComment 模型中为反向关系添加一个相关的查询名称

Firstly in your userComment model add a related query name for reverse relation

class UserComment(models.Model):
    sender = models.ForeignKey(User, related_name='sender', related_query_name='s')
    recipient = models.ForeignKey(User, related_name='recipient', related_query_name='r')
    sent_at = models.DateTimeField(auto_now_add=True)
    comment = models.TextField()

现在在你的 views.py 中使用这个查询:

Now in your views.py use this query:

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,))

根据模型所在的app的名称在extra中设置appname.

Set the appname in extra according to name of the app in which the model is.

现在您可以按如下方式访问它:

Now you can access it as follows:

for user in users:
    print user.username
    print user.last_message_time
    print user.message

这篇关于如何向用户对话显示每个用户的最后一条消息以保留聊天记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆