It absolutely isn't, I'm afraid.
customer = Customer.objects.get(user=request.user)
posts = Post.objects.filter(mentions__in=customer).order_by('-created_at')
Would fail at mentions__in = customer
because the __in
lookup expects an iterable (which a single customer is not).
Aside from that, that query would give you all posts in which customer
was mentioned, which could also be achieved in two more straight forward ways:
posts = Post.objects.filter(mentions=customer).order_by('-created_at')
posts = customer.mentions.order_by('-created_at') # using the 'related_name' from the customer's side
You want to get all the users that are mentioned in a post. But what post? You forgot to mention that in your question. You only gave us the current user (request.user
), who can have multiple posts.
I'm going to guess and show how you could get all other users mentioned in posts made by the current user.
To make things clearer in respect to the related_name
of that relation, I will change it to related_name = 'mentionend'
.
posts = Post.objects.filter(mentions=customer) # all posts of the current user
# all other users mentioned in those posts
users = Customer.objects.exclude(user=customer).filter(mentionend__in=posts) # using 'related_name'
# or
users = posts.mentions.exclude(user=customer)