我正在比赛管理器上,并且开始在比赛系统上工作。我的问题是如何退回两支球队的所有组合,然后才能使用这些组合进行比赛?
让我解释一下。一队仅属于一个池。在“团队”模型中,
我有一个外键字段,引用了该团队所属的池。以我的算法,我想创建一个属于同一个池的两支球队参加的比赛。例如,我有A,B,C,D团队,我想创建A对抗B,A对抗C,A对抗D等。
我在这里看了一下:https://docs.python.org/3/library/itertools.html,发现了“组合”功能,仅此而已。
models.py
class Match(models.Model):
scoreTeam1 = models.IntegerField()
scoreTeam2 = models.IntegerField()
phase = models.ForeignKey(Phase, default=None, on_delete=models.CASCADE)
teams = models.ManyToManyField(Team, default=None, blank=True)
class Pool(models.Model):
name = models.CharField(max_length=16)
nbTeam = models.IntegerField(validators=[
MaxValueValidator(4),
MinValueValidator(3)
])
phase = models.ForeignKey(Phase, default=None, on_delete=models.CASCADE)
field = models.ForeignKey(Field, default=None, on_delete=models.CASCADE)
class Team(models.Model):
name = models.CharField(max_length=16)
totalpoints = models.IntegerField(default=0)
position = models.IntegerField(default=0)
category = models.ForeignKey(Category, default=None, on_delete=models.CASCADE)
pool = models.ForeignKey(Pool, default=None, blank=True, null=True, on_delete=models.SET_NULL)
views.py
def matches_phase_view(request, id, id_phase, *args, **kwargs):
tournaments = Tournament.objects.filter(user=request.user)
tournament = Tournament.objects.get(pk=id)
phase = Phase.objects.get(pk=id_phase)
pools = Pool.objects.filter(phase=phase)
teams = Team.objects.filter(pool__in=pools)
matches_count = 0
for pool in pools:
matches_count = matches_count + pool.nbTeam * (pool.nbTeam - 1) // 2
for pool in pools:
teams_combinations = combinations(Team.objects.filter(pool=pool), 2)
list_teams_combinations = list(teams_combinations)
for combination in list_teams_combinations:
match = Match.objects.create(phase=phase)
match.teams.add(combination[0])
match.teams.add(combination[1])
match.save()
matches = Match.objects.filter(phase=phase)
context = {
'tournaments': tournaments,
'tournament': tournament,
'pools': pools,
'teams': teams,
'matches_count': matches_count,
'matches': matches,
}
return render(request, 'tournament_manage_phase_matches.html', context)
我尝试过:
for pool in pools:
a = combinations(Team.objects.filter(pool=pool), 2)
但是我遇到了这个错误:
未定义名称“组合”
如果有人有更好的主意,我想听听。
我正在使用Python 3.7,Django 2.2。
编辑:
template
{% extends 'base_tournament.html' %}
{% load crispy_forms_tags %}
{% block title %}
Tournament
{% endblock %}
{% block navbar_manage %}
{% include 'navbar_manage.html' %}
{% endblock %}
{% block content %}
<h1>Matches of ...</h1>
<ul class="nav nav-tabs" id="myTab" role="tablist">
{% for pool in pools %}
<li class="nav-item">
<a class="nav-link" id="{{ pool.name }}-tab" data-toggle="tab" href="#{{ pool.name }}" role="tab" aria-controls="{{ pool.name }}" aria-selected="true">{{ pool.name }}</a>
</li>
{% endfor %}
</ul>
<div class="tab-content" id="myTabContent">
{% for pool in pools %}
<div class="tab-pane fade" id="{{ pool.name }}" role="tabpanel" aria-labelledby="{{ pool.name }}-tab">
{% for match in matches %}
<p>Wait for a moment</p>
{% endfor %}
</div>
{% endfor %}
</div>
{% endblock %}
答案 0 :(得分:1)
from itertools import combinations