使用 Django ORM 计算多对多关系的频率和相关性的优雅方法?
我有一个 Pizza 模型和一个 Topping 模型,两者之间具有多对多关系。
你能推荐一种优雅的方式来提取:
- 每个的流行度(频率) 最高
- 之间的相关性 配料(即哪组 配料是最常见的)
谢谢
I have a Pizza model and a Topping model, with a many-to-many relationship between the two.
Can you recommend an elegant way to extract:
- the popularity (frequency) of each
topping - the correlation between
toppings (i.e. which sets of
toppings are most frequent)
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更新:找到了一种更好的方法,为连接表使用单独的模型。考虑这样的关系:
现在你可以这样做:
原始答案:
我以前遇到过类似的情况。就我而言,模型是
Unit
和Weapon
。他们之间存在多对多的关系。我想统计一下武器的受欢迎程度。这就是我的做法:我认为您可以对
Pizza
和Topping
做同样的事情。我怀疑可能还有其他(更好)的方法来做到这一点。Update: Found a better way using a separate model for the join table. Consider a relationship like this:
Now you can do this:
Original Answer:
I faced a similar situation before. In my case the models were
Unit
andWeapon
. They had a many to many relationship. I wanted to count the popularity of weapons. This is how I went about it:I think you can do the same for
Pizza
andTopping
. I suspect there might be other (better) ways to do it.