B
B
bituke2021-10-29 15:05:00
Django
bituke, 2021-10-29 15:05:00

How to use the or operator in the filter function?

I understand that this is not done as I want to do)
I want to get all objects in which the user is either the sender or the recipient.
model:

class Dialogue(models.Model):
  sender = models.ForeignKey(User, related_name="user_sender", on_delete=models.CASCADE)
  reciever = models.ForeignKey(User, related_name="user_reciever", on_delete=models.CASCADE)
  created = models.DateTimeField(auto_now_add=True)


What I want to do:
Dialogue.objects.filter(sender=request.user or reciever=request.user)

Something like this) How is this solved?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Nesterov, 2021-10-29
@bituke

from django.db.models import Q
q1 = Q(sender=request.user)
q2 = Q(reciever=request.user)
D = Dialogue.objects.filter(q1|q2)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question