django——如何分离一个queryset?

时间:2021-08-17 15:26:20

I am creating a messaging app, between the users. Hope fully with the help, I came up with the business logic, I think will work. So now, I can send message and get all the messages (sent and received) for a particular user (eg. A). But I am stuck in querying or separating that messages (all the sent/received messages of A) by each user (eg. messages sent/received by A and B, messages sent/received by A and C). Below is my models.py. Please have a look and kindly help me out. Or if there's a better way out please advise me. Will be much appreciated. Thank you.

我正在用户之间创建一个消息应用程序。希望在充分的帮助下,我想出了商业逻辑,我想会行得通的。所以现在,我可以发送消息并获取特定用户的所有消息(发送和接收)。A).但我被每个用户(如A的所有发送/接收消息)所困,无法查询或分离该消息(如A的所有发送/接收消息)。A和B发送/接收的消息,A和C发送/接收的消息。请您看一看,并好心地帮助我。或者如果有更好的办法,请建议我。将更加感激。谢谢你!

models.py

models.py

class Thread(models.Model):
    sender = models.ForeignKey(User, related_name="sender_set")
    recipient = models.ForeignKey(User, related_name="recipient")
    message = models.TextField()
    date = models.DateTimeField(default=datetime.now)

# To get all the messages of A
>>> conversation = Thread.objects.filter(Q(sender=A) | Q(recipient=A))
[<Thread: A to B><Thread: C to A><Thread: B to A><Thread: A to C><Thread: A to B>]

How to get separate conversation for each user? Hope I have cleared my point. Thank you.

如何为每个用户获得单独的对话?希望我已经阐明了我的观点。谢谢你!

2 个解决方案

#1


1  

I encourage you not to reinvent the wheel. I had a similar task recently and I've found it very easy to simply adopt the code from the excellent user_messages project. In order to adopt and understand what I was doing I needed to understand the DB model utilized, which is the following:

我鼓励你不要重新发明*。我最近有一个类似的任务,我发现简单地采用来自优秀user_messages项目的代码非常容易。为了采用和理解我所做的事情,我需要了解所使用的DB模型,如下所示:

django——如何分离一个queryset?

Basically your User model (whether the standard one or the one you override the standard one with) has:

基本上,您的用户模型(无论标准模型还是使用标准模型覆盖的模型)有:

  • 1:N (one to many) Message
  • 1:N(一到多)信息
  • 1:N (one to many) UserThread
  • 1:N(1到多个)用户线程

A Thread has 1:N UserThread, which makes it easy to group the messages under threads, depending on which user you are displaying the thread to.

一个线程有1:N个UserThread,这使得在线程下对消息进行分组变得很容易,这取决于您将线程显示给哪个用户。

Modeling the data in this way makes the retrieval of separate conversations for each user (what you ask) trivial.

以这种方式建模数据使得为每个用户(您所要求的)检索单独的会话变得微不足道。

Please look into their models.py and related managers.py. You will realize the good design decisions they've made and put into the code. I had to extend it myself since it didn't cater for all my use cases. But I did really find it easy to extend.

请看看他们的模型。py和相关managers.py。您将认识到他们所做的良好设计决策并将其放入代码中。我必须自己扩展它,因为它不能满足我所有的用例。但我确实发现它很容易扩展。

By the way: I found user_messages by looking at the django packages already available for messages using the extremely helpful djangopackages.com website.

顺便说一下:我通过查看django包找到了user_message,这些包已经可以使用非常有用的djangopackages.com网站提供消息了。

#2


0  

(eg. messages sent/received by A and B, messages sent/received by A and C)

If i understand correctly your question; what you want is to get all messages between 2 users(sent or recieved). So based on your model design, these 2 queries gives you what you want:

如果我正确地理解了你的问题;您需要的是在两个用户之间获取所有消息(发送或接收)。基于你的模型设计,这两个问题给出了你想要的:

c1 = Thread.objects.filter(sender=A, recipient=B)
c2 = Thread.objects.filter(sender=B, recipient=A)

total = list(c1) + list(c2) # this will give you all messages between A and B

You can create a function to handle this for multiple specific users:

您可以为多个特定用户创建一个函数来处理此问题:

def separate_messages(user1, user2):
    c1 = Thread.objects.filter(sender=user1, recipient=user2)
    c2 = Thread.objects.filter(sender=user2, recipient=user1)

    total = list(c1) + list(c2)
    return total

users = [B, C, D, E]
const_user = A

for user in users:
    print separate_messages(const_user, user)

#1


1  

I encourage you not to reinvent the wheel. I had a similar task recently and I've found it very easy to simply adopt the code from the excellent user_messages project. In order to adopt and understand what I was doing I needed to understand the DB model utilized, which is the following:

我鼓励你不要重新发明*。我最近有一个类似的任务,我发现简单地采用来自优秀user_messages项目的代码非常容易。为了采用和理解我所做的事情,我需要了解所使用的DB模型,如下所示:

django——如何分离一个queryset?

Basically your User model (whether the standard one or the one you override the standard one with) has:

基本上,您的用户模型(无论标准模型还是使用标准模型覆盖的模型)有:

  • 1:N (one to many) Message
  • 1:N(一到多)信息
  • 1:N (one to many) UserThread
  • 1:N(1到多个)用户线程

A Thread has 1:N UserThread, which makes it easy to group the messages under threads, depending on which user you are displaying the thread to.

一个线程有1:N个UserThread,这使得在线程下对消息进行分组变得很容易,这取决于您将线程显示给哪个用户。

Modeling the data in this way makes the retrieval of separate conversations for each user (what you ask) trivial.

以这种方式建模数据使得为每个用户(您所要求的)检索单独的会话变得微不足道。

Please look into their models.py and related managers.py. You will realize the good design decisions they've made and put into the code. I had to extend it myself since it didn't cater for all my use cases. But I did really find it easy to extend.

请看看他们的模型。py和相关managers.py。您将认识到他们所做的良好设计决策并将其放入代码中。我必须自己扩展它,因为它不能满足我所有的用例。但我确实发现它很容易扩展。

By the way: I found user_messages by looking at the django packages already available for messages using the extremely helpful djangopackages.com website.

顺便说一下:我通过查看django包找到了user_message,这些包已经可以使用非常有用的djangopackages.com网站提供消息了。

#2


0  

(eg. messages sent/received by A and B, messages sent/received by A and C)

If i understand correctly your question; what you want is to get all messages between 2 users(sent or recieved). So based on your model design, these 2 queries gives you what you want:

如果我正确地理解了你的问题;您需要的是在两个用户之间获取所有消息(发送或接收)。基于你的模型设计,这两个问题给出了你想要的:

c1 = Thread.objects.filter(sender=A, recipient=B)
c2 = Thread.objects.filter(sender=B, recipient=A)

total = list(c1) + list(c2) # this will give you all messages between A and B

You can create a function to handle this for multiple specific users:

您可以为多个特定用户创建一个函数来处理此问题:

def separate_messages(user1, user2):
    c1 = Thread.objects.filter(sender=user1, recipient=user2)
    c2 = Thread.objects.filter(sender=user2, recipient=user1)

    total = list(c1) + list(c2)
    return total

users = [B, C, D, E]
const_user = A

for user in users:
    print separate_messages(const_user, user)