6
\$\begingroup\$

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.

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

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:

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.