Bastian
Bastian

Reputation: 5855

Django query to filter out current user from a list of users

I have a query that returns a list of users. It contains the current logged in user and I would like to make the same query without returning this user. In my models Book has a foreignkey to Library called library and Library has a m2m to users called subscribers. It's probably not important as a simple query to all users should probably work for the example as well.

users = book.library.subscribers.all()

request.user ends up in users too and I don't want that. I know I could take it out of the list after making the query but I would like to know if there is directly a way to exclude request.user from the query.

Upvotes: 0

Views: 1454

Answers (1)

karthikr
karthikr

Reputation: 99660

You can do

book.library.subscribers.exclude(user=request.user) 

Assuming, user is the attribute that stores the users

Upvotes: 1

Related Questions