试图找到最常见的五个条目 [英] Trying to find top 5 most common entries
本文介绍了试图找到最常见的五个条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从查询中找到受伤最多的玩家,但是我无法获得正确的结果。
I am trying to find the most injured player from my query but I am having trouble getting the proper results.
我正在考虑将玩家ID放在列表,但是你如何计算重复的条目,然后产生最受伤的前5名列表?
I was thinking of putting the player ID's in a list but how do you go about counting duplicate entries and then producing a "top 5" most injured list?
这是我的models.py
Here is my models.py
class PlayerInjury(models.Model):
player = models.ForeignKey(Player)
injury_type = models.ForeignKey(Injury)
injury_date = models.DateField(verbose_name='Injured On', null=True, blank=True)
description = models.CharField(verbose_name='Description', max_length=180, null=True, blank=True)
status = models.ForeignKey(Status)
projected_return = models.DateField(verbose_name='Projected Return Date', null=True, blank=True)
hide = models.BooleanField(default=False)
returned = models.BooleanField(default=False)
timestamp = models.DateTimeField(auto_now_add=True)
到目前为止,我的views.py
EDIT
and what I have so far for my views.py
EDIT
def home(request):
context={}
player_list = []
most_recent = PlayerInjury.objects.all().order_by('-timestamp')[:5]
news = News.objects.all()
most_injured = PlayerInjury.objects.annotate(injury_count=Count('id')).order_by('-injury_count')[:5]
context['most_injured'] = most_injured
context['most_recent'] = most_recent
context['news'] = news
return render_to_response('dash/home.html', RequestContext(request, context))
推荐答案
为什么不使用注释?
from django.db.models import Count
Player.objects.annotate(injury_count=Count('playerinjury')).order_by('-injury_count')[:5]
这篇关于试图找到最常见的五个条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文