如何获取查询集中对象的数量? (姜戈)

发布于 2024-10-27 10:10:52 字数 509 浏览 2 评论 0原文

如何添加相关对象计数字段?

class Item(models.Model):
    name = models.CharField()

class Contest(models.Model);
    name = models.CharField()

class Votes(models.Model):
    user = models.ForeignKey(User)
    item = models.ForeignKey(Item)
    contest = models.ForeignKey(Contest)
    comment = models.TextField()

查找 contestA 的投票:

current_vote = Item.objects.filter(votes__contest=contestA)

它返回包含所有投票的查询集。如何获取每个 Item 的相关投票数?

How can I add a field for the count of related objects?

class Item(models.Model):
    name = models.CharField()

class Contest(models.Model);
    name = models.CharField()

class Votes(models.Model):
    user = models.ForeignKey(User)
    item = models.ForeignKey(Item)
    contest = models.ForeignKey(Contest)
    comment = models.TextField()

To find the votes for contestA:

current_vote = Item.objects.filter(votes__contest=contestA)

which returns a queryset with all of the votes. How do I get the count of related votes for each Item?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

与往事干杯 2024-11-03 10:10:52

要获取特定项目的票数,您可以使用:

vote_count = Item.objects.filter(votes__contest=contestA).count()

如果您想详细了解特定竞赛中的票数分布,我会执行如下操作:

contest = Contest.objects.get(pk=contest_id)
votes   = contest.votes_set.select_related()

vote_counts = {}

for vote in votes:
  if not vote_counts.has_key(vote.item.id):
    vote_counts[vote.item.id] = {
      'item': vote.item,
      'count': 0
    }

  vote_counts[vote.item.id]['count'] += 1

这将创建将项目映射到票数的字典。这不是唯一的方法,但它对数据库的命中率相当低,因此运行速度很快。

To get the number of votes for a specific item, you would use:

vote_count = Item.objects.filter(votes__contest=contestA).count()

If you wanted a break down of the distribution of votes in a particular contest, I would do something like the following:

contest = Contest.objects.get(pk=contest_id)
votes   = contest.votes_set.select_related()

vote_counts = {}

for vote in votes:
  if not vote_counts.has_key(vote.item.id):
    vote_counts[vote.item.id] = {
      'item': vote.item,
      'count': 0
    }

  vote_counts[vote.item.id]['count'] += 1

This will create dictionary that maps items to number of votes. Not the only way to do this, but it's pretty light on database hits, so will run pretty quickly.

享受孤独 2024-11-03 10:10:52

另一种方法是使用聚合。您应该能够使用单个查询获得类似的结果。例如:

from django.db.models import Count

Item.objects.values("contest").annotate(Count("id"))

我没有测试这个特定的查询,但这应该以字典的形式输出竞赛中每个值的项目计数。

Another way of doing this would be using Aggregation. You should be able to achieve a similar result using a single query. Such as this:

from django.db.models import Count

Item.objects.values("contest").annotate(Count("id"))

I did not test this specific query, but this should output a count of the items for each value in contests as a dictionary.

伴梦长久 2024-11-03 10:10:52

使用相关名称来计算特定竞赛的选票

class Item(models.Model):
    name = models.CharField()

class Contest(models.Model);
    name = models.CharField()

class Votes(models.Model):
    user = models.ForeignKey(User)
    item = models.ForeignKey(Item)
    contest = models.ForeignKey(Contest, related_name="contest_votes")
    comment = models.TextField()

>>> comments = Contest.objects.get(id=contest_id).contest_votes.count()

Use related name to count votes for a specific contest

class Item(models.Model):
    name = models.CharField()

class Contest(models.Model);
    name = models.CharField()

class Votes(models.Model):
    user = models.ForeignKey(User)
    item = models.ForeignKey(Item)
    contest = models.ForeignKey(Contest, related_name="contest_votes")
    comment = models.TextField()

>>> comments = Contest.objects.get(id=contest_id).contest_votes.count()
2024-11-03 10:10:52

您可以使用 len() 来获取contestA的票数:

current_vote = len(Item.objects.filter(votes__contest=contestA))

实际上,len()select_for_update() 如下所示:

current_vote = len(Item.objects.select_for_update().filter(votes__contest=contestA))

因为 select_for_update() 不使用 count() 如下所示:

current_vote = Item.objects.select_for_update().filter(votes__contest=contestA).count()

您可以看到 我的回答解释了 select_for_update()count( )len()

You can use len() to get the count of contestA's votes:

current_vote = len(Item.objects.filter(votes__contest=contestA))

Actually, len() is used with select_for_update() as shown below:

current_vote = len(Item.objects.select_for_update().filter(votes__contest=contestA))

Because select_for_update() doesn't work with count() as shown below:

current_vote = Item.objects.select_for_update().filter(votes__contest=contestA).count()

You can see my answer explaning about select_for_update() with count() or len().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文