多对多关系:查询具有单个值的对象
我有两个型号。一个 Artist
模型和一个 Album
模型。有一个 ManyToManyField
连接两者,因为一张专辑可以有多个艺术家(合作等)。
我遇到的问题是,当我执行简单的 Album.objects.filter(artists=1)
类型查询时,它预计会显示具有以下属性的所有 Album
对象:那位艺术家归因于它。我想要做的是找到该艺术家是唯一艺术家的所有专辑(他们的发行版,没有合作)。
(我不熟悉 extra() 子句,但我确信它可能与此问题的解决方案有关,因此非常感谢这方面的任何帮助。)
I have two models. An Artist
model, and a Album
model. There's a ManyToManyField
connecting the two, since an album can have more than one artist (collaborations and the like).
The problem I'm having is when I do a simple Album.objects.filter(artists=1)
-type query, it expectedly shows all of the Album
objects that have that artist attributed to it. What I want to do is find all of the albums where that artist is the only artist (their releases, no collaborations).
(I'm not familiar with the extra()
clause, but I'm sure it might have something to do with the solution to this, so any help on that end is much appreciated.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基本上,这个想法是找到所有恰好有 1 位艺术家的专辑(通过注释和过滤),然后进行过滤。
Basically the idea is to find all the albums who have exactly 1 artist (by annotating and filtering), and then filter down.