gpt4 book ai didi

python - 如何返回 Django 中两个团队的所有组合?

转载 作者:太空宇宙 更新时间:2023-11-04 04:12:07 24 4
gpt4 key购买 nike

我正在研究锦标赛管理器,并且开始研究比赛系统。我的问题是如何返回两支球队的所有组合,然后我才能用这些组合创建比赛?
让我解释一下。一个团队只属于一个池。在“团队”模型中,我有一个引用团队所属池的外键字段。在我的算法中,我想创建由属于同一池的两支球队进行的比赛。例如我有 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)

但是我遇到了这个错误:

name 'combinations' is not defined

如果有人有更好的主意,我想听听。

我正在使用 Python 3.7、Django 2.2。

编辑:

模板

{% 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 %}

最佳答案

从 itertools 导入组合

关于python - 如何返回 Django 中两个团队的所有组合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56131759/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com