Reputation: 383
How to filter the model where 2 fields are attributed to the user, where in these 2 fields you can be, how to filter 1 by calling and output all in one variable.
models.py
class Conversation(models.Model):
my = models.ForeignKey(UserProfile, related_name='My')
friend = models.ForeignKey(UserProfile, related_name='Friend')
short_message = models.ForeignKey("Message", related_name="Short_Message")
timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
updated = models.DateTimeField(auto_now_add=False, auto_now=True)
I can be in these 2 fields, but how to pull all 1 at a time, that is, there is a model where I write messages and where I to output through one...
I need to know if such a model with the field my = username and suddenly there is such a model where I will be that friend = username that is I can be in 2 fields how to pull all at once? So that there are not many cycles!
Upvotes: 0
Views: 40
Reputation: 778
use Q
https://docs.djangoproject.com/en/1.11/topics/db/queries/#complex-lookups-with-q
Conversation.objects.filter(Q(my=user)|Q(friend=user))
should work
Upvotes: 2