models.py:
from django.db import models
class Server(models.Model):
name = models.CharField(max_length=16)
url = models.CharField(max_length=64)
class Team(models.Model):
server = models.ForeignKey(Server, on_delete=models.CASCADE)
name = models.CharField(max_length=16)
n_players = models.IntegerField(null=True)
class Player(models.Model):
server = models.ForeignKey(Server, on_delete=models.CASCADE)
team = models.ForeignKey(Team, on_delete=models.CASCADE)
name = models.CharField(max_length=16, null=True)
highlight = models.BooleanField(default=False)
views.py:
from django.shortcuts import render
from .models import Server, Team
def index(request):
server_list = Server.objects.order_by('name')[:3]
context = {'server_list': server_list}
return render(request, 'monitor/index.html', context)
templates index.html:
{% for server in server_list %}
<h2>{{ server.name }}</h2>
<table>
<tr>
{% for team in server.team_set.all %}
<th>
<b>{{ team }}</b>
</th>
{% endfor %}
</tr>
{% for team in server.team_set.all %}
<tr>
{% for player in team.player_set.all %}
<td>{{ player }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
{% endfor %}
Вопрос в том, как сделать так (не меняя модель данных), чтобы Игроки одной команды распределялись правильно в одном столбце по командам, а не в строчку как сейчас.