简体   繁体   English

Django查询以从用户列表中过滤出当前用户

[英]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. 在我的模型中,Book有一个到库的外键,称为库,而库有一个到用户的m2m,称为订户。 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. request.user也会出现在用户中,我不希望那样。 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. 我知道我可以在查询后将其从列表中删除,但我想知道是否有直接从查询中排除request.user的方法。

You can do 你可以做

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

Assuming, user is the attribute that stores the users 假设用户是存储用户的属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM