2

我有模型:

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' 不在 Player 模型中

4

1 回答 1

0

用这个:

players = team.playerteam_set.filter(date_end__isnull=True).values_list('player', flat=True)
于 2012-05-20T15:41:35.253 回答