group by

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# -*- coding: utf-8
from django.contrib.auth.models import User
from django import template
from profile.models import Message
register = template.Library()
@register.inclusion_tag('menu.html', takes_context=True)
def new_messages(context):
user = context['user']
tmp_messages = Message.objects.filter(recipient=user, read=False)
authors_list = []
for message in tmp_messages:
authors_list.append(message.sender)
authors = set(authors_list)
messages = []
for author in authors:
messages.append(author.sender.filter(read=False)[0])
return {'messages': messages}