我正在尝试构建一个消息传递应用。这是我的模特:
"cartalyst/stripe-laravel": "5.0.*"
这是我在视图中尝试的内容:
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)).order_by('-created_at')
在这里,我想根据{% for abc in data %}
{{ abc.receiver }} <br/>
{% endfor %}
最近向谁发送新消息(正如我们在社交媒体平台上看到的那样)过滤不同的接收者并对其进行重新排序。
我该怎么做?
答案 0 :(得分:0)
过滤 request.user 发送的消息,按receiver_id
和-created_at
排序,然后在receiver_id
上使用distinct
Message.objects.filter(sender=request.user).order_by('receiver_id','-created_at').distinct('receiver_id')