2014-09-24 92 views
0

的領域我有這樣的模式:比較同型號

#Model for match result forecast 
class ResultForecast(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='forecasted_match') 

所以我想要得到的地方去home_goals比客場進球,反之亦然更大的物體,我用這個:

total_forecast = ResultForecast.objects.filter(match=match).count() 
home_trend = ResultForecast.objects.filter(match=match, home_goals__gt=F('away_goals')) 
away_trend = ResultForecast.objects.filter(match=match, away_goals__gt=F('home_goals')) 

但它不起作用

回答

1

我想知道如果匹配應該實際上match__exact?你有沒有試過以下看看它是否有所作爲?我也更喜歡使用Q類。 https://docs.djangoproject.com/en/1.7/ref/models/queries/

from django.db.models import Q 

    home_trend = ResultForecast.objects.filter(
     Q(match__exact=match) & 
     Q(home_goals__gt=F('away_goals'))) 

    away_trend = ResultForecast.objects.filter(
     Q(match__exact=match) & 
     Q(away_goals__gt=F('home_goals')))