另一个新手问题,我害怕。我有3个列表的这种形式。我们的目标是改变查询集的列表2(见视图代码)django - queryset差异 - 不按预期工作
form.py
class AddGameForm(forms.ModelForm):
won_lag = forms.ChoiceField(choices=[('1','Home') , ('2', 'Away') ],)
home_team = forms.ModelChoiceField(queryset=Player.objects.all(),required=True)
away_team = forms.ModelChoiceField(queryset=Player.objects.all(), required=True)
class Meta:
model = Game
fields =()
view.py
def game_add(request, match_id):
# used to create a new Game
""" return http response page for creating a new Game """
# Adding a new Game so just make a new one
game = Game()
# Get the match specified in the Querystring because we will need it to figure out who the home team
# and away team are.
try:
match = Match.objects.get(id=match_id)
except Match.DoesNotExist:
# we have no object! do something
#todo: redirect to list match page?
pass
# get form
form = AddGameForm(request.POST or None, instance=game)
# Change the Queryset for Home and Away team to show only players that are on the team
# AND have not already played in a game so we need to get the "DIFFERENCE" between all
# team members and any players that have already played in the match
home_players = Player.objects.filter(team=match.home_team) # All Home Team members
away_players = Player.objects.filter(team=match.away_team) # All Away Team members
already_played_in_match_players = Game.objects.filter(match=match) # This is both home and away players
# that have played in a Game on this match
form.fields['home_team'].queryset = home_players.exclude(pk__in=already_played_in_match_players)
form.fields['away_team'].queryset = away_players.exclude(pk__in=already_played_in_match_players)
...
在我的数据库我有以下几种:
Team 1
Player 1
Player 2
Team 2
Player 3
Match 1
no games
因此当我打开表单时,如预期的那样,home_team列表显示Player1,Player2和away_team列表显示Player3
因此,我选择Player 1和Player3并保存游戏。 现在DB具有以下数据
Team 1
Player 1
Player 2
Team 2
Player 3
Match 1
Game 1 between Player1 and Player3
我决定再添加一个比赛,我打开GameAddForm,并期望有HOME_TEAM列表只显示Player2和AWAY_TEAM列表中显示没有球员。
然而,在现实中,什么情况是,HOME_TEAM列表作为预期,但的AWAY_TEAM列表中仍然显示播放器3.
我,为什么它能够正常工作,对主队完全糊涂了,但不是客队。
有什么建议吗?
在此先感谢您提供的任何指导。