gpt4 book ai didi

Django 聚合选择

转载 作者:行者123 更新时间:2023-12-05 00:22:28 27 4
gpt4 key购买 nike

我有以下模型:

class VotingRound(models.Model):
pass # here are some unimportant fields

class Vote(models.Model):
voting_round = models.ForeignKey(VotingRound)
vote = models.CharField(choices=...)

现在我有 VotingRound 的实例,我想知道每个值代表了多少次。这可以通过 collections.Counter 轻松完成:
>>> Counter(voting_round_instance.vote_set.values_list('vote', flat=True))
Counter({u'decline': 8, u'neutral': 5, u'approve': 4})

现在我想知道是否有办法使用 Django 聚合技术来做到这一点....

我找到了 this module ,但在使用它之前我想知道是否有本地方式来做到这一点。

最佳答案

是的你可以!

from django.db.models import Count
voting_round_instance.vote_set.values('vote') \
.annotate(count=Count('vote')).distinct()

编辑:使用 order_by()
您可能还需要确保默认排序不会弄乱您的聚合。在使用相关对象管理器时尤其如此。

https://docs.djangoproject.com/en/1.8/topics/db/aggregation/#interaction-with-default-ordering-or-order-by

Fields that are mentioned in the order_by() part of a queryset (or which are used in the default ordering on a model) are used when selecting the output data, even if they are not otherwise specified in the values() call. These extra fields are used to group “like” results together and they can make otherwise identical result rows appear to be separate. This shows up, particularly, when counting things.


from django.db.models import Count
voting_round_instance.vote_set.values('vote') \
.annotate(count=Count('vote')) \
.distinct().order_by()

关于Django 聚合选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29836920/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com