S
S
stayHARD2015-09-20 00:15:17
PostgreSQL
stayHARD, 2015-09-20 00:15:17

Django distinct or how to discard mutual Foreign Key duplicates when using Q(OR)?

Hello. You need to get a list of recent messages for a specific user. I do it like this:

Message.objects.filter(
            Q(user_sender_id=token.user_id) |
            Q(user_receiver_id=token.user_id)).order_by('user_sender', 'user_receiver').distinct('user_sender', 'user_receiver')

After such a request, I get:
udDMrv-l43JnR5J6FyI3wIRy2ejqk5vvR51fOsVY
A should receive only:
1-3, 2-1 messages.
What am I doing wrong? Or am I using distinct incorrectly?
It is necessary to discard all mutual pairs (leave only one - the last one).

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question