这是models.py
class CompetitionEntry(models.Model):
submitter = models.ForeignKey(User)
pic = models.ImageField(upload_to=images, blank=True, null=True)
class CompetitionEntryVote(models.Model):
voted_entry = models.ForeignKey(CompetitionEntry)
class Entrylikes(models.Model):
ip_address = models.IPAddressField()
liked_entry = models.ForeignKey(CompetitionEntry)
这是views.py(我认为问题就在这里)
def show_all_entries(request, id):
entries = CompetitionEntry.objects.filter(competition__id__exact=comp.id).annotate(vote_count=Count('competitionentryvote'), likes_count=Count('entrylikes'))
return render(request, "show_all.html", {
"entries": entries,
})
show_all.html
{% for item in entries %}
Votes = {{item.vote_count}} Likes= {{item.likes_count}}
{% endfor %}
这里的问题是输出中的投票和喜欢都是相同的。i,e 投票=喜欢=喜欢
如果我重写视图以仅显示投票或喜欢中的一个,则该页面可以完美运行。
在views.py中,如果我使用
entries = CompetitionEntry.objects.filter(competition__id__exact=comp.id).annotate(vote_count=Count('competitionentryvote')).annotate(likes_count=Count('entrylikes'))
我得到与上面1相同的结果