型号:
class MyTeam(models.Model):
user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
match = models.ForeignKey(Match, on_delete=models.SET_NULL, null=True, related_name='match_set')
mega_league = models.ForeignKey(MegaLeague, on_delete=models.SET_NULL, null=True, blank=True, related_name='mega_league_set')
captain_first_team = models.ForeignKey(FirstTeamPlayer, on_delete=models.SET_NULL, null=True, blank=True, related_name="captain_second_team_set")
captain_second_team = models.ForeignKey(SecondTeamPlayer, on_delete=models.SET_NULL, null=True, blank=True, related_name="captain_second_team_set")
team_player_first_team = models.ManyToManyField(FirstTeamPlayer, blank=True, related_name="team_player_first_team_set")
team_player_second_team = models.ManyToManyField(SecondTeamPlayer, blank=True, related_name="team_player_second_team_set")
首先,我需要在一个字段中添加这些captain_first_team
和captain_second_team
(例如在一个字段中添加FirstTeamPlayer
和SecondTeamPlayer
)。怎么样?
观看次数:
def add_to_my_team_from_team_create(request, match_id):
match = get_object_or_404(Match, pk=match_id)
my_team = MyTeam.objects.create(match=match, user=request.user)
return redirect('team_create', match.pk, my_team.pk)
def team_create(request, match_id, team_id):
match = get_object_or_404(Match, pk=match_id)
my_team = get_object_or_404(MyTeam, pk=team_id)
first_team_player = match.first_team.first_team_player.all()
second_team_player = match.second_team.second_team_player.all()
context = {
'match': match,
'my_team': my_team,
'first_team_player': first_team_player,
'second_team_player': second_team_player,
}
return render(request, 'main/team_create.html', context=context)
def add_first_team_player(request, match_id, team_id, player_id):
match = get_object_or_404(Match, pk=match_id)
my_team = get_object_or_404(MyTeam.objects.filter(user=request.user, match=match.pk), pk=team_id)
first_team_player = get_object_or_404(match.first_team.first_team_player, pk=player_id)
my_team.team_player_first_team.add(first_team_player)
return redirect('team_create', match.pk, my_team.pk)
def remove_first_team_player(request, match_id, team_id, player_id):
match = get_object_or_404(Match, pk=match_id)
my_team = get_object_or_404(MyTeam.objects.filter(user=request.user, match=match.pk), pk=team_id)
first_team_player = get_object_or_404(match.first_team.first_team_player, pk=player_id)
my_team.team_player_first_team.remove(first_team_player)
return redirect('team_create', match.pk, my_team.pk)
在视图中,首先,我使用MyTeam
函数创建一个add_to_my_team_from_team_create
,然后使用add_first_team_player
在字段上添加数据,还可以使用remove_first_team_player
从字段中删除数据。但是我需要相反的一个。也就是说,我需要添加字段,然后在模板中按保存按钮时,它将保存在MyTeam
中。
team_create模板:
{% for first_team_player in first_team_player %}
<tr id="wk">
<td>
<img src="{{ first_team_player.player_image.url }}">
<div>
{{ first_team_player.player_name }} <br>
{{ match.first_team.name }}
</div>
</td>
<td>
{% if first_team_player in my_team.team_player_first_team.all %}
<a href="{% url 'remove_first_team_player' match.pk my_team.pk first_team_player.pk %}"><button type="button" class="btn btn-danger btn-sm">Remove -</button></a>
{% else %}
<a href="{% url 'add_first_team_player' match.pk my_team.pk first_team_player.pk %}"><button type="button" class="btn btn-primary btn-sm">Add +</button></a>
{% endif %}
</td>
</tr>
{% endfor %}
<a href=""><button type="button" class="btn btn-primary btn-sm">Save</button></a>
当我按下Add +
按钮然后将此播放器添加到MyTeam
模型中时,在模板中;当按下Remove -
按钮时此播放器将从该模型中删除。
现在我的问题全是2分:
在创建MyTeam
时,我想在此提出一些逻辑。我需要来自1个团队的最多7名玩家(当最大玩家添加然后Add +
按钮将被禁用)和来自另一1个团队的其他玩家,总共玩家将为11(当具有总玩家添加然后Add +
按钮将被禁用对于所有玩家)。首先,Save
按钮保持禁用状态,当MyTeam
中有11位玩家完成比赛时,它将启用,按下这些玩家将保存到MyTeam
。这样,一个QuerySet就可以创建。
如果我创建MyTeam
1st并在其字段中添加数据,则它可能是一个空团队。但是在这里我不能空手。因此,第一,我需要在字段上添加数据,然后按Save
按钮,它将创建一个包含所有数据的新团队。
我该如何解决这2个问题?
为了更好地理解,我想像这样 team create of dream11 。 在调查了给定的链接后,请详细帮助我。