Django aggregate, use of AND/OR operators in when condition
<p>With the following queryset I would like to add and / or condition in the when clause, but I get syntax error.
Is it possible to do it in a single queryset or should I split?</p>

<pre><code>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') &amp; team_home_id=625 |
      score_away__gt=F('score_home') &amp; team_away_id=625, then=1), 
      default=0, output_field=IntegerField()
    )),
  )
</code></pre>

<p>According to the <a href="https://docs.djangoproject.com/en/1.10/ref/models/conditional-expressions/#when" rel="nofollow">docs</a> you have to use <code>Q</code> objects when using complex conditions (just like you did in <code>filter</code>). An example:</p>

<pre><code>When(Q(name__startswith="John") | Q(name__startswith="Paul"), then='name')
</code></pre>

python django aggregate django-queryset