Sunday, January 3, 2010

Re: Baseball statistics

Thanks alot! This was really helpful, and yes you're right, the g
field is redundant.

Thanks again!

On Jan 3, 6:36 pm, greatlemer <greatle...@googlemail.com> wrote:
> On Jan 3, 2:36 pm, Tijmen <tijmen.vandenbr...@gmail.com> wrote:
>
>
>
> > Hi there,
>
> > First of all let me say I'm just starting here but need some help with
> > a rather basic question.
>
> > I want to make an app that holds baseball statistics for players of
> > each game and over several seasons. Now I'm new to this db
> > relationship thing and would like to hear your opinion.
>
> > My models.py currently looks like this:
>
> > <code>
> > from django.db import models
>
> > # Create your models here.
> > class Player(models.Model):
> >     GENDER_CHOICES = (
> >         ('M', 'Male'),
> >         ('F', 'Female'),
> >     )
>
> >     BATS_CHOICES = (
> >         ('R', 'Right'),
> >         ('L', 'Left'),
> >         ('B', 'Both'),
> >     )
>
> >     THROWS_CHOICES = (
> >         ('R', 'Right'),
> >         ('L', 'Left'),
> >         ('B', 'Both'),
> >     )
>
> >     first_name = models.CharField(max_length=50)
> >     last_name = models.CharField(max_length=50)
> >     gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
> >     bats = models.CharField(max_length=1, choices=BATS_CHOICES)
> >     throws = models.CharField(max_length=1, choices=THROWS_CHOICES)
> >     birthdate = models.DateTimeField('birth date')
> >     awards = models.CharField(max_length=200)
> >     height = models.IntegerField()
> >     weight = models.IntegerField()
> >     email = models.CharField(max_length=100)
> >     tel = models.CharField(max_length=50)
>
> >     def __unicode__(self):
> >         #return self.first_name, self.last_name, self.gender
> >         return self.first_name + " " +  self.last_name
>
> > class Game(models.Model):
> >     #players = models.ManyToManyField(Player)           #Player
> >     team = models.CharField(max_length=200)             #Team for
> > which the player plays
> >     league = models.CharField(max_length=200)           #League
> >     opponent = models.CharField(max_length=200)         #The opponent
> >     date = models.DateTimeField('date')                 #Date
>
> >     def __unicode__(self):
> >         return self.team + " vs " + self.opponent
>
> > class Season(models.Model):
> >     year = models.DateTimeField('year')
>
> >     def __unicode__(self):
> >         return self.year
>
> > class Statistic(models.Model):
> >     players = models.ManyToManyField(Player)
> >     games = models.ManyToManyField(Game)
> >     g = models.IntegerField()                           #Games played
> > or pitched
> >     pa = models.IntegerField()                          #Plate
> > appearances estimated using AB + BB + HBP + SF + SH missing catcher
> > interferences
> >     ab = models.IntegerField()                          #At bats
> >     r = models.IntegerField()                           #Runs scored/
> > allowed
> >     h = models.IntegerField()                           #Hits/hits
> > allowed
> >     doubles = models.IntegerField()                     #Doubles hit/
> > allowed
> >     triples = models.IntegerField()                     #Triples hit/
> > allowed
> >     hr = models.IntegerField()                          #Home Runs hit/
> > allowed
> >     rbi = models.IntegerField()                         #Runs Batted
> > In
> >     sb = models.IntegerField()                          #Stolen Bases
> >     cs = models.IntegerField()                          #Caught
> > Stealing
> >     bb = models.IntegerField()                          #Bases on
> > Balls/Walks
> >     so = models.IntegerField()                          #Strikeouts
> >     ba = models.IntegerField()                          #Hits/At bats
> >     obp = models.IntegerField()                         #On Base
> > Percentage (H + BB + HBP)/(AB + BB + HBP + SF)
> >     slg = models.IntegerField()                         #Slugging,
> > Total Bases/At Bats (1B + 2 * 2B + 3 * 3B + 4 * HR)/AB
> >     ops = models.IntegerField()                         #On base +
> > Slugging Percentages
> >     opsplus = models.IntegerField()                     #100*[OBP/lg
> > OBP + SLG/lg SLG -1]
> >     tb = models.IntegerField()                          #Total Bases
> >     gdp = models.IntegerField()                         #Double Plays
> > Grounded Into
> >     hbp = models.IntegerField()                         #Times Hit by
> > a Pitch
> >     sh = models.IntegerField()                          #Sacrifice
> > Hits/Bunts
> >     sf = models.IntegerField()                          #Sacrifice
> > Flies
> >     ibb = models.IntegerField()                         #Intentional
> > Bases on Balls
> >     pos = models.CharField(max_length=15)               #Positions
>
> >     def __unicode__(self):
> >         return "Stats"
> > </code>
>
> > As you can see I have 4 classes defined:
> >  - player
> >  - game
> >  - statistic
> >  - season
>
> > A game is played with several players, each player has statistics for
> > this game and a game is part of a season. Now I'm a bit lost in what
> > kind of relationship I should use. models.ManyToManyField or
> > models.ForeignKey... And where I should put the statement.
>
> > Any help would be greatly appreciated.
>
> > Thanks
>
> > - Tijmen
>
> Hi,
>
> First of all, I just wanted to confirm that a statistic represents a
> player's performance for a single game (it sounded like that from your
> question at the end but was unclear thanks to the "g" field on the
> Statistic model), I'll assume that it does.
> In this case you should have the following relationships:
>
> Since a game occurs in a single season, you want to have a ForeignKey
> in the Game model that represents the season that game occurred in.
> If Statistic does represent a stat for a specific player in a specific
> game then it should contain two foreign keys, one to Player and one to
> Game.
> Finally you can add a ManyToMany field to either Player or Game (it
> doesn't matter which one it goes on, but should only be on one) and
> you can specify that Statistic is the "through" table [1].  You could
> do that like this:
> players = models.ManyToManyField(Player, through='Statistic',
> related_name='games') # This would go in Game
> games = models.ManyToManyField(Game, through='Statistic',
> related_name='players') # This would go in Player
> The related_name argument will be the name given to the relationship
> on the other model, so with either of these you would have access to
> games on the Player model and players on the Game model.
>
> Hope this helps.
>
> [1]http://docs.djangoproject.com/en/dev/topics/db/models/#intermediary-m...
>
> --
>
> G

--

You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate