198๐
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.
26๐
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.
- [Django]-Django admin ManyToMany inline "has no ForeignKey to" error
- [Django]-Negating a boolean in Django template
- [Django]-Django โ How to rename a model field using South?
2๐
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()
- [Django]-How to do math in a Django template?
- [Django]-Authenticate by IP address in Django
- [Django]-Pylint "unresolved import" error in Visual Studio Code
0๐
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()
.
- [Django]-Django Template Language: Using a for loop with else
- [Django]-How can I build multiple submit buttons django form?
- [Django]-">", "<", ">=" and "<=" don't work with "filter()" in Django