How to filter multiple messages in django
so i recently did a filter for messages so as to bring all the messages between two people
def pmessage(request, pk):
user = User.objects.get(id=pk)
message = pm.objects.filter(Q(receiver=request.user, sender=user) | Q(receiver=user, sender=request.user))
form = dmform()
context = {
'message' : message,
'form' : form,
}
it works, only that it groups the messages in categories ie, it would list all the messages that meets one criteria before the other irrespective of which message came first, and that defeats my aim.
thanks you
You can order the messages with .order_by('created')
:
def pmessage(request, pk):
message = pm.objects.filter(
Q(receiver=request.user, sender_id=pk) |
Q(receiver_id=pk, sender=request.user)
).order_by('created')
form = dmform()
context = {
'message' : message,
'form' : form,
}