2014-09-24 95 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')))