Django-私人讯息对话检视 [英] Django - Private messaging conversation view

查看:56
本文介绍了Django-私人讯息对话检视的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为Django项目构建了一个非常基本的私人消息传递模块.

I have built a very basic private messaging module for my Django project.

我有一个包含以下内容的消息模型:

I have a Message model which consists of:

sender (Foreign key to the member model)
recipient (Foreign key to the member model)
message
date (Datetime of which the message was created)

现在我的问题是我想创建一个新视图,该视图根据这些消息返回一个对话列表.

Now my issue is i would like to create a new view which returns a list of conversations based on these messages.

我正在尝试编写一个查询,该查询返回最新消息,但在发件人=当前用户或收件人=当前用户的情况下是唯一的.这是为了让我有最新消息的列表,该列表应该等效于对话列表.我说得对吗,还是我完全在思考问题?

I am trying to write a query which returns the latest messages but unique where sender = current user OR recipient = current user. This is so that i have a list of latest messages, which should be the equivalent of a conversation list. Have i got that bit right or am i completely over thinking things?

conversations = Message.objects.filter(Q(recipient=request.user) | 
Q(sender=request.user)).annotate(max=Max('date'))

但是这返回了重复的对话,我是从另一个堆栈溢出帖子中尝试过的:

but this is returning duplicate conversations I tried this from another stack overflow post:

conversations = Message.objects.order_by('recipient', 'sender', 
'date').distinct('recipient', 'sender') 

但是我收到此错误此数据库后端不支持DISTINCT ON字段"

but I'm receiving this error "DISTINCT ON fields is not supported by this database backend"

任何帮助将不胜感激.

推荐答案

如@Grimmy在评论中所述,请发布更多信息.具体来说,请在您的答案中添加您尝试过的查询(在注释中已经包含了该查询)和结果,以及结果出了什么问题(您说重复的转换;您的意思是每次对话仅显示两次或多次)多过两次?)

As @Grimmy stated in the comments, please post more information. Specifically, please add to your answer the query you tried (which you already have in the comments) and the result, and what is wrong with the result (you say "duplicate converstations; do you mean each conversation shows up just twice, or more than twice?)

在这种情况下,有两种选择可供考虑:

Two options come to mind for this situation:

  1. 您可以在现有模型上运行查询,以删除/排除重复项.
  2. 您可以创建一个名为 Conversation 的新模型,该模型可以保存有关对话的信息,包括成员,开始日期等,并向每条消息添加一个外键,从而为其分配一个对话(很多-从消息到对话的一个).这样可以简化您的查询,并且可以轻松扩展以适应群聊"(如果需要)(只需向对话中添加更多成员)或对话本身的图片或标题之类的东西.
  1. You could run a query on your existing model that removes/excludes duplicates.
  2. You could create a new model called Conversation which holds information about the conversation, including members, date started, etc., and add a foreign key to each message assigning it one conversation (many-to-one from message to conversation). This would simplify your queries and would be easily extendable to accommodate "group chats" if need be (just add more members to the conversation), or things like a picture or title for the conversation itself.

当您发布更多信息时,如果您愿意,我将详细介绍选项一.现在,我要说的是,如果您遇到重复的会话,而每个会话仅显示两次(一次是用户是发送者,一次是用户是接收者),那么听起来您的查询就足够了,并且您只需在python中编写一个for循环即可对产生的对话进行排序,并删除较旧的重复项.只要每个用户进行的对话最多不超过一两百次(听起来像是这种情况),就不会过多降低性能.

When you post more information, I'll address option one in more detail if you would like. For now, I'll just say that if you are getting duplicate conversations where each conversation only shows up twice (once where the user is the sender and once where the user is the recipient), then it sounds like your query is good enough and you can just write a for loop in python that sorts through the resulting conversations and removes the older duplicates. As long as each user is in no more than over one or two hundred conversations, which sounds like the case, that shouldn't slow down performance too much.

但是无论如何,我建议选择第二种方法.我必须在Django项目中实现类似的功能,并且选择了选项2.它极大地简化了查询,使其看起来像这样:

But in any event, I recommend option two. I had to implement a similar feature in a Django project, and I went with option 2. It greatly simplifies your query to look like this:

# get the conversations the user is in
conversation_list = Message.objects.filter(conversation__participants=user).order_by('-date')

# get a list of the most recent message of each conversation
message_list = conversation_list.values('conversation').annotate(
    first_msg=Max('conversation__message')
)

为了使第二行正确地对消息进行排序,请将以下内容添加到您的消息模型中:

In order for that second line to sort messages properly, add the following to your Message model:

class Message(models.Model):
    # sender field is unchanged
    # date field is unchanged (maybe rename to avoid name collisions with date module)
    # message field is unchanged (maybe rename to 'content' to avoid confusion)

    # make recipient many-to-many because I'd recommend having both the sender and the recipient listed as recipients,
    # but you don't have to do that
    recipient = models.ManyToManyField(User)

    # conversation foreign key
    conversation = models.ForeignKey(Conversation, blank=False, null=False)

    # this simplifies sorting so the "Max('conversation__message')" line 
    # sorts on date rather than primary key
    ordering = ["-date"]

这是会话模型:

class Conversation(models.Model):
    participants = models.ManyToManyField(User)

    # example functionality you may wish to add later
    group_name = models.CharField(max_length=512, default="Group", blank=False, null=False)
    profile_picture = models.FileField(upload_to='uploads/', default='uploads/GroupChatIcon.jpg')

这篇关于Django-私人讯息对话检视的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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