是否可以在Django中编写自引用聚合?
例如,给定以下模型定义:
from django.db import models
class Match(models.Model):
match_id = models.BigIntegerField(primary_key=True)
start_time = models.DateTimeField()
league = models.ForeignKey(League, on_delete=models.CASCADE)
team = models.ForeignKey(Team, on_delete.models.CASCADE)
我想为每个Match
对象添加先前匹配的数量。 (并最终在其他条件下,例如,与给定球队比赛的先前比赛。)
我最初的想法是:
from django.db.models import Q, F, Count
matches = Match.objects.filter(team__name='xyz')
matches.annotate(
prior_matches=Count('match_id', filter=(
Q(start_time__lt=F('start_time')
))
)
不幸的是,这似乎为所有对象提供了prior_matches=0
。
This question建议遵循一个外键并使用反向关系,但这似乎很笨拙,而且还存在两个问题:
Match
对象都用作外键,但这似乎并不理想。
matches.annotate(
prior_matches=Sum(Case(
When(start_time__lt=F('league__matches__start_time'), then=1),
default=0,
output_field=models.IntegerField()
))
)
matches
查询集是为特定团队过滤的,上面的代码也会找到所有先前匹配的计数。我想不出一个好办法(除了重复在每个Q
/ filter
子句中应用于查询集的完整过滤条件,这在管理器中是不通用的)。为了避免xy问题,我想通过在数据库级别工作来替换的(伪)代码片段之一如下:
def generate_stats(match):
matches = Match.objects.filter(start_time__lt=match.start_time)
for team in (match.home_team, match.away_team):
q_involving = Q(home_team=team) | Q(away_team=team)
team_matches = matches.filter(q_involving)
team_matches.aggregate(...) # get stats
答案 0 :(得分:0)
我认为您应该将第一个代码更改为
from django.db import models
class Match(models.Model):
match_id = models.AutoField(primary_key=True)
start_time = models.DateTimeField()
league = models.ForeignKey(League, on_delete=models.CASCADE)
team = models.ForeignKey(Team, on_delete.models.CASCADE)
否则,可能是您实际上没有添加“匹配”