我正在尝试构建一个消息传递应用。这是我的模特,
class Message(models.Model):
sender = models.ForeignKey(User, related_name="sender")
receiver = models.ForeignKey(User, related_name="receiver")
msg_content = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
这是我在视野中尝试的,
data = Message.objects.filter(Q(sender=request.user) | Q(receiver=request.user))
在模板中,
{% for abc in data %}
{{ abc.receiver }} <br/>
{% endfor %}
如我们在社交媒体平台上看到的那样,如何根据新消息获取不同用户并重新排序?
答案 0 :(得分:0)
你可以试试这段代码
inbox_message = Message.objects.filter.objects.filter(receiver=request.user).order_by('-created_at')
outbox_message = Message.objects.filter.objects.filter(sender=request.user).order_by('-created_at')
答案 1 :(得分:0)
这里你需要做的事情,
request.user
rec - somename 为上述发件人获取receivedMsg,接收者request.user
serder - somename
message_queue = Message.objects.filter(Q(receiver = rec_name,sender = request.user),Q(receiver = request.user,sender = rec_name).order_by(&#39; -created_at&#39;)< / p>
按创建者的顺序将像链一样随机播放