使用through在ManyToManyField中过滤

时间:2012-05-20 15:21:57

标签: django

我有模特:

class Player(models.Model) :
    ...
    teams = models.ManyToManyField("Team", through="PlayerTeam")

class Team(models.Model) :
    ...
    players = models.ManyToManyField("Player", through="PlayerTeam")

class PlayerTeam(models.Model) :
    player = models.ForeignKey(Player)
    team = models.ForeignKey(Team)
    date_begin = models.DateField()
    date_end = models.DateField(blank=True, null=True)

我想让所有参加选定球队的球员(date_end为空)。我试过这个:

team = get_object_or_404(Team, pk=id)
players = team.players.filter(date_end__isnull=True)

但它不起作用,因为字段'date_end'不在播放器模型中

1 个答案:

答案 0 :(得分:0)

使用此:

players = team.playerteam_set.filter(date_end__isnull=True).values_list('player', flat=True)