有没有办法根据 ManyToManyField 检查 request.user
我有一个小问题,想知道是否有人可以帮助我。我正在尝试检查 request.user 是否是 ManyToMany 关系表中的用户之一。有办法做到这一点吗?
示例:
我想检查用户是否正在关注业务页面,如果是,则让他们能够取消关注,反之亦然。
我有:
followers = models.ManyToManyField(User, related_name="Followers", null=True, blank=True)
只是想知道是否有一种方法可以根据关注者表检查 request.user.id 的特定 ID。
谢谢大家。
史蒂夫
I have a small problem and wondering if someone can help me out. I'm trying to do a check to see if the request.user is one of the user's in the ManyToMany relationship table. Is there a way to accomplish this?
Example:
I want to check if the user is following the business page, and if they are, give them the ability to unfollow it and vice versa.
I have:
followers = models.ManyToManyField(User, related_name="Followers", null=True, blank=True)
Just wondering if theres a way to check the request.user.id against the followers table for that specific id.
Thanks everyone.
Steve
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这种“djangoic”方式来做到这一点:
更“pythonic”方式,不推荐,因为它在数据库方面效率非常低:(
后一个将所有用户数据加载到python中,而第一个使用数据库查询)
This "djangoic" way to do it:
The more "pythonic" way, which is not recommended because it is very inefficient db wise:
(This later one loads all users data into python, while the first one uses a db query)
你应该可以轻松检查
you should be easily able to check