如何在不同的项目上添加注释? [英] How to add an annotation on distinct items?
问题描述
我有一个查询...
packages = Package.objects.annotate(bid_count = Count('items__bids '))
哪个应该给我一个包的数量列表,每个出价的数量。如果包中只有一个项目,那么它的效果会很好,但是如果还有两个项目,则会重复计算。
每个包由1个或更多个项目组成。每个出价都放在一个包装内的一个或多个物品上。我想检索该包中的物品的出价数量。
如果包装中有2件商品有1个出价,目前这个数量将算作2我想要它返回1。
我试过 Count('items__bids__distinct')
但是没有工作。我如何做到这一点?
我有同样的问题,我在这里找到了解决方案: http://www.djangofoo.com/182/filter-annotate-count
packages = Package.objects.annotate(bid_count = Count('items__bids',distinct = True))
/ pre>
I've got a query...
packages = Package.objects.annotate(bid_count=Count('items__bids'))
Which is supposed to give me a list of packages with the number of bids each. It works great if there's only one item in the package, but if there's more it double counts.
Each package consists of 1 or more items. Each bid is placed on 1 or more items within a package. I want to retrieve the number of bids placed on the items within that package.
If there is 1 bid placed on 2 items within a package, presently this will count as 2, I want it to return 1.
I tried
Count('items__bids__distinct')
but that didn't work. How can I do this?解决方案I had the same problem and I found the resolution here: http://www.djangofoo.com/182/filter-annotate-count
packages = Package.objects.annotate(bid_count=Count('items__bids', distinct = True))
这篇关于如何在不同的项目上添加注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!