user1870343
user1870343

Reputation:

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

I'm not sure how to ask this question ? First let me explain i have Team and Player models.

class team()
        name = models.CharField(max_length=100) // name quato something like this

class player(models.Model) 
        point = models.IntegerField()
        assist = models.IntegerField() 
        rebound = models.IntegerField() //something like this 
        team = models.ForeignKey(team)

I need a Match model and players ( point,assist,rebound ) for that match; how do I create a model to represent it?

Upvotes: 2

Views: 1688

Answers (2)

Bolo
Bolo

Reputation: 11690

Is this an exercise/homework with simplifying assumptions, or are you modelling a real-world application? For example, players may change teams over time: how will you model it? For simplicity, let us assume that players don't change teams. I would model your case as follows:

class Team(models.Model):
    name = models.CharField(max_length=100)
    # other attributes that do not change over time

class Player(models.Model):
    name = models.CharField(max_length=100)
    team = models.ForeignKey(Team)
    # other attributes that do not change over time

class Match(models.Model):
    venue = models.CharField(max_length=100)
    date = models.DateField()
    # other non-redundant attributes

class PlayerMatch(models.Model):
    player = models.ForeignKey(Player)
    match = models.ForeignKey(Match)

    points = models.IntegerField()
    assists = models.IntegerField()
    rebounds = models.IntegerField()

The important part here is that statistics are kept in PlayerMatch only. You don't have to repeat them in Player or in Match, that would be redundant. Instead, statistics for a match or for a player can be calculated given these data structures.

Upvotes: 2

Brandon Taylor
Brandon Taylor

Reputation: 34553

I would do:

class StatisticsBase(models.Model):
    class Meta:
        abstract = True

    points = models.IntegerField()   # plural form reads easier here
    assists = models.IntegerField()  # at least in English
    rebounds = models.IntegerField()


class Player(StatisticsBase):
    name = models.CharField(max_length=100)


class Match(StatisticsBase):
    players = models.ManyToManyField(Player)  # you should capitalize class names
    # any additional fields

Upvotes: 1

Related Questions