2014-09-22 27 views
0

组足球结果我有这个模型了足球比赛的结果经理:通过分数

#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

使用分组一定的匹配人群的结果:

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')) 
+0

很好,非常感谢! – 2014-09-23 00:36:47