How to query multiple described objects exist in a many to many field
How do I check whether there is a thread containing only the sender (user 1) and the recipient (user 2), and no other users.
models.py
class Thread(models.Model):
user = models.ManyToManyField(User)
is_hidden = models.ManyToManyField(User, related_name='hidden_thread', blank=True)
class Message(models.Model):
thread = models.ForeignKey(Thread)
sent_date = models.DateTimeField(default=datetime.now)
sender = models.ForeignKey(User)
body = models.TextField()
is_hidden = models.ManyToManyField(User, related_name='hidden_message', blank=True)
I tried
>>> Thread.objects.filter(user=user1&user2)
>>> Thread.objects.filter(user=user1|user2)
# Both gave me an error: Unsupported operand.
# Then this
>>> Thread.objects.filter(Q(user=user1) & Q(user=user2))
# Which gave me no threads at all.
# Then this
>>> Thread.objects.filter(Q(user=user1) | Q(user=user2)).distinct()
# Gave me threads of both the users.
What I want is to check the thread, with the specified users only. Suppose, User 1 wants to send a message to User 2. What I want is, first check whether there is a thread between both the users. If there is, get that thread, or else create a new one. How is it possible? What is the best way to do it. Please help me. Thank you.
And please tell me what's the difference between | and &? Because I had very different results with these two.
-- models.py
class Thread(models.Model):
user = models.ManyToManyField(User)
is_hidden = models.ManyToManyField(User, related_name='hidden_thread', blank=True)
class Message(models.Model):
thread = models.ForeignKey(Thread)
sent_date = models.DateTimeField(default=datetime.now)
sender = models.ForeignKey(User)
body = models.TextField()
is_hidden = models.ManyToManyField(User, related_name='hidden_message', blank=True)
I tried
>>> Thread.objects.filter(user=user1&user2)
>>> Thread.objects.filter(user=user1|user2)
# Both gave me an error: Unsupported operand.
# Then this
>>> Thread.objects.filter(Q(user=user1) & Q(user=user2))
# Which gave me no threads at all.
# Then this
>>> Thread.objects.filter(Q(user=user1) | Q(user=user2)).distinct()
# Gave me threads of both the users.
What I want is to check the thread, with the specified users only. Suppose, User 1 wants to send a message to User 2. What I want is, first check whether there is a thread between both the users. If there is, get that thread, or else create a new one. How is it possible? What is the best way to do it. Please help me. Thank you.
And please tell me what's the difference between | and &? Because I had very different results with these two.
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHSNPWtFESu7WGZcHDOeNCVc1FqOqxqw2kkiRY0jaJctXy6XHQ%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home