HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythondjangoMinor

Model structure for Player Team and Match in football application

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
applicationplayerteammatchfootballforstructureandmodel

Problem

I'm creating an application for foosball matches.

I have models like below:

class Player(models.Model):
    match_amount = models.IntegerField(default=0)
    wins = models.IntegerField(default=0)
    avatar = models.ImageField(blank=True)
    user = models.OneToOneField(
        on_delete=models.CASCADE,
        to=User,
        primary_key=True,
        related_name='player',
        verbose_name=_('user'),
    )

    def __str__(self):
        return self.user.get_full_name()

class Team(models.Model):
    players = models.ManyToManyField(Player)
    wins = models.IntegerField(default=0)

    def __str__(self):
        return "Team %s" % self.pk

class Match(models.Model):
    date = models.DateField(default=now)

    def __str__(self):
        return "Match on %s" % self.date

class TeamMatch(models.Model):
    team = models.ForeignKey(Team)
    match = models.ForeignKey(Match)

    points = models.IntegerField()

    def __str__(self):
        return "Match %s" % self.team


Is these models are well created?

Thanks much for each tip and corrections.

Solution

It is difficult to say how good your models are, since it depends on your application requirements and demands, current and future/planned use cases.

But, few points from the top of my head:

  • there are some important fields missing - for example, team names



-
I don't particularly like the TeamMatch model. Instead, I'd expect the Match to have links to the home and away teams:

class Match(models.Model):
    home_team = models.ForeignKey(Team, related_name='home_matches')
    away_team = models.ForeignKey(Team, related_name='away_matches')

    date = models.DateField(default=now)

    def __str__(self):
        return "Match between '%s' and '%s' on %s" % (self.home_team.name, self.away_team.name, self.date)


-
you will also need some way of keeping the score of a match. A separate MatchResult model?

  • how about a use case when a player moves from one team to another? If this is something you want to keep track of, you would probably need something like a PlayerContract model to relate players and teams for a certain period of time



Also, please see these similar model design discussions:

  • How should I design my django models for team, player and match objects?



  • Models for a team

Code Snippets

class Match(models.Model):
    home_team = models.ForeignKey(Team, related_name='home_matches')
    away_team = models.ForeignKey(Team, related_name='away_matches')

    date = models.DateField(default=now)

    def __str__(self):
        return "Match between '%s' and '%s' on %s" % (self.home_team.name, self.away_team.name, self.date)

Context

StackExchange Code Review Q#156485, answer score: 3

Revisions (0)

No revisions yet.