Enes
Enes

Reputation: 341

How can i store tournament standings

i have a model class

class MobaTournament(Tournament):
    teams=models.ManyToMany(Team)

if a tournament have 20 teams. I want to sort of rank 1st team, 2nd team, 3rd team... How do you suggest I do this? like;

1.XXX team
2.XXX team
3.XXX team

Upvotes: 2

Views: 76

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476699

You can store that in the through=... table [Django-doc]. Like:

class MobaTournament(Tournament):
    teams = models.ManyToMany(Team, through='MobaTeam')


class MobaTeam(models.Model):
    moba_tournament = models.ForeignKey(MobaTournament, on_delete=models.CASCADE)
    team = models.ForeignKey(Team, on_delete=models.CASCADE)
    points = models.IntegerField(default=0)

Basically if there exists a MobaTeam with a given moba_tournament and a given team, then that team thus is a member of the teams of that moba_tournament and vice versa.

We can thus store, and update the points of that team for that tournament accordingly. You can for example retrieve the teams with the corresponding points with:

from django.db.models import F

Team.objects.filter(
    mobateam__moba_tournament=some_tournament
).annotate(
    points=F('mobateam__points')
).order_by(
    '-points'
)

Upvotes: 2

Related Questions