比较相同型号的字段

时间:2014-09-24 01:19:27

标签: django mongodb orm

我有这个型号:

#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')

所以我想得到de 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 个答案:

答案 0 :(得分:1)

我想知道匹配是否应该匹配_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')))