Django 聚合,在 when 条件下使用 AND/OR 运算符

Django aggregate, use of AND/OR operators in when condition

对于以下查询集,我想在 when 子句中添加和/或条件,但出现语法错误。 是否可以在单个查询集中完成,还是应该拆分?

Game.objects.prefetch_related('schedule').filter(Q(team_home_id=625) | Q(team_away_id=625), schedule__date_time_start__lte=timezone.now())
  .aggregate(
    wins=Sum(Case(When(
      score_home__gt=F('score_away') & team_home_id=625 |
      score_away__gt=F('score_home') & team_away_id=625, then=1), 
      default=0, output_field=IntegerField()
    )),
  )

根据 docs,在使用复杂条件时您必须使用 Q 对象(就像您在 filter 中所做的那样)。一个例子:

When(Q(name__startswith="John") | Q(name__startswith="Paul"), then='name')