仅显示与同一用户Django相关的最后未读消息

时间:2019-05-05 10:02:12

标签: django django-models django-views django-template-filters

我有一个通知下拉列表,它在登录用户所属的任何线程中显示未读消息。

例如Sam和Jane。

简与3个不同的人在3个线程中。 Sam连续发送了6条消息。现在,通知下拉列表会显示来自Sam的所有6条未读邮件,效率不高。

即使红色未读通知将显示6,下拉菜单也应仅显示来自Sam的最后一条未读消息。

我遇到了麻烦,因为我不知道要提取/显示最后一条消息时要过滤的内容。 Notification模型只有3个字段。

class Notification(models.Model):
    notification_user = models.ForeignKey(User, on_delete=models.CASCADE)
    notification_chat = models.ForeignKey(ChatMessage, on_delete=models.CASCADE)
    notification_read = models.BooleanField(default=False)

用户发送消息时,消息另存为Notification对象,接收用户的消息另存为notification_user

所以现在我有这个功能

def notification(request):
    if request.user.is_authenticated:
        notification = Notification.objects.filter(notification_user=request.user, notification_read=False)
        notification_read = Notification.objects.filter(notification_user=request.user, notification_read=True)
        return {
            'notification':notification,
            'notification_read':notification_read
        }
    return Notification.objects.none()

哪个显示与登录用户关联的所有read/unread通知,但是显然显示了来自Sam的所有6条消息。

models.py

class Thread(models.Model):
    first        = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='chat_thread_first')
    second       = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='chat_thread_second')
    updated      = models.DateTimeField(auto_now=True)
    timestamp    = models.DateTimeField(auto_now_add=True)

    objects      = ThreadManager()

class ChatMessage(models.Model):
    thread      = models.ForeignKey(Thread, null=True, blank=True, on_delete=models.SET_NULL)
    user        = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name='sender', on_delete=models.CASCADE)
    message     = models.TextField()
    timestamp   = models.DateTimeField(auto_now_add=True)

consumers.py(创建通知的地方)

  @database_sync_to_async
    def get_thread(self, user, other_username):
        return Thread.objects.get_or_new(user, other_username)[0]

    @database_sync_to_async
    def create_chat_message(self, me, msg):
        thread_obj = self.thread_obj
        return ChatMessage.objects.create(thread=thread_obj, user=me, message=msg)

    @database_sync_to_async
    def create_notification(self, other_user, msg):
        last_chat = ChatMessage.objects.latest('id')
        created_notification = Notification.objects.create(notification_user=other_user, notification_chat=last_chat)
        return created_notification

示例navbar.html

{% for notifications in notification_read|slice:"0:6" %}
                  <li><a href="{% url 'thread' user %}">
                    <span id="notification-{{notification.id}}">
                      '{{ notifications.notification_chat.message }}'
                      -{{ notifications.notification_chat.user }}
                    </span>
                  </a></li>

1 个答案:

答案 0 :(得分:1)

Notification.objects.filter(
    notification_user=request.user, notification_read=True
).order_by('-notification_chat__timestamp')[:1]

别忘了添加正确的order_by,否则“最后记录”将是随机的。