Answer the question
In order to leave comments, you need to log in
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)
Dialogue.objects.filter(sender=request.user or reciever=request.user)
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question