按得分分组足球比赛结果

时间:2014-09-22 21:27:39

标签: django mongodb orm mongoengine

我有一个足球结果经理的模型:

#Model for football matchs
class Match(models.Model):
    name = models.CharField(max_length=200)
    slug = models.SlugField(max_length=200, blank=True, null=True)
    match_date = models.DateTimeField('match date', blank=True, null=True)

    home = models.ForeignKey(Team, related_name='home')
    away = models.ForeignKey(Team, related_name='away')

    home_goals = models.DecimalField(max_digits=5, decimal_places=0, blank=True, null=True)
    away_goals = models.DecimalField(max_digits=5, decimal_places=0, blank=True, null=True)

    league = models.ForeignKey(League)
    season = models.ForeignKey(Season, blank=True, null=True)

    def __unicode__(self):
        return unicode(self.name)

    def save(self, *args, **kwargs):
      if not self.id:  
        self.slug = slugify(self.name)
      super(Match, self).save(*args, **kwargs)




#Model for crowd matches results
class CrowdResult(models.Model):
    user = models.ForeignKey(User)
    created_date = models.DateTimeField('match date', blank=True, null=True)
    home_goals = models.DecimalField(max_digits=5, decimal_places=0, blank=True, null=True)
    away_goals = models.DecimalField(max_digits=5, decimal_places=0, blank=True, null=True)
    match = models.ForeignKey(Match, related_name='match')

所以我想得到与主场/客场进球分组的某场比赛相关的人群结果

1 个答案:

答案 0 :(得分:1)

使用:

from django.db.models import Count
CrowdResult.objects.values('match', 'home_goals', 'away_goals').order_by().annotate(Count('match'), Count('home_goals'), Count('away_goals'))