I have a pretty large site that tracks a football team and it's league ( http://www.muskegohitmen.com ).
What I did was to have an abstract base statistics class that is attached to a player/user. then mad a class for every stat i wanted to track class Statistics(models.Model): #base class (abstract?) # for_player = models.ForeignKey(User, related_name = "%(class)s_related") for_player = models.ForeignKey(Member, related_name = "%(class)s_related") for_game = models.ForeignKey (Game,related_name = "%(class)s_related" ) date_stamp = models.PositiveIntegerField (editable=False) def save(self, force_insert=False, force_update=False): ds = self.for_game.date_scheduled self.date_stamp=ds.year super(Statistics, self).save(force_insert, force_update) class Meta: abstract = True @models.permalink def get_absolute_url(self): return '%d/%d"%d/statistics/' % (self.for_game.date_scheduled.year,self.for_game.date_scheduled.month,self.for_game.date_scheduled.day) get_absolute_url = permalink(get_absolute_url) #=============================================================================== # OFFENSE # ALL % AND AVG ARE DONE AUTOMATICALLY - NO NEED TO INCLUDE THEM HERE #=============================================================================== class PassingStatistics(Statistics): # passing passing_attempts = models.PositiveSmallIntegerField ('Attempts',default = 0) passing_completions = models.PositiveSmallIntegerField ('Completions',default = 0) passing_tot_yards = models.PositiveSmallIntegerField ('Total Yards',default = 0) passing_tds = models.PositiveSmallIntegerField ('Touchdowns', default = 0) passing_interceptions = models.PositiveSmallIntegerField ('Interceptions', default = 0) class Meta: verbose_name = 'Passing Statistics' verbose_name_plural = 'Passing Statistics' def passing_percentage(self): if self.passing_attempts == 0: return 0 else: return (( float(self.passing_completions) / float (self.passing_attempts) ) * 100) #====================== get_qb_rating ===================================== # if no statsDict is passed, we calculate QB rating for the current Game instance # Can easily re - use to calc for a span of games, career, season, etc # http://en.wikipedia.org/wiki/Passer_rating #========================================================================================= def get_qb_rating(self, statsDict=None): # store in variables to cut on memory / stack overflow & Processor cycles """ C = Passing Completions Y = Passing Yards Per Attempt T = Passing Touchdowns Per Attempt I + Interceptions Per Attempts Pass in a dictionary named statsDict if the Passing Statisitcs you wish to calculate statsDict["completions"] statsDict["attempts"] statsDict["yards"] statsDict["touchdowns"] statsDict["interceptions"] If no dictionary is passed, the function will calculate a QB rating for current game the stats associated with. C = ( (self.passing_completions / self.passing_attempts)*100 - 30) / 20 Y = ((self.passing_tot_yards / self.passing_attempts)-3) * (1/4) T = (self.passing_tds / self.passing_attempts) * 20 I = 2.375 - ( (self.passing_interceptions / self.passing_attempts) * 25 ) rating = (( max(min(C, 2.375),0 ) + max(min(Y,2.375),0) + max (min(T, 2.375),0) + max(min(I,2.375),0))/ 6) *100 """ if statsDict is not None: C = float(( (float(statsDict['completions']) / float (statsDict['attempts'])*100 - 30)) / 20 ) Y = ((statsDict['yards'] / statsDict['attempts']) - 3) /4 T = float((statsDict['touchdowns']) / float(statsDict ['attempts']) * 20) I = float(2.375 - ((float(statsDict['interceptions']) / float(statsDict['attempts'])) * 25 )) rating = (( max(min(C, 2.375),0 ) + max(min(Y,2.375),0) + max(min(T, 2.375),0) + max(min(I,2.375),0))/ 6) *100 return rating C = float(( (float(self.passing_completions) / float (self.passing_attempts)*100 - 30)) / 20 ) Y = ((self.passing_tot_yards / self.passing_attempts) - 3) /4 T = float((float(self.passing_tds) / float (self.passing_attempts)) * 20) I = float(2.375 - ((float(self.passing_interceptions) / float (self.passing_attempts)) * 25 )) rating = float((( max(min(C, 2.375),0 ) + max(min(Y,2.375),0) + max(min(T, 2.375),0) + max(min(I,2.375),0))/ 6) *100) return rating class RushingStatistics(Statistics): # rushing rushing_carries = models.SmallIntegerField ('Carries',default = 0) rushing_total_yards = models.PositiveSmallIntegerField ('Total Yards',default=0) rushing_yac = models.PositiveSmallIntegerField ('Yds After Contact',default = 0,) rushing_td = models.SmallIntegerField ('Touchdowns',default = 0) rushing_fumbles = models.PositiveSmallIntegerField ('Fumbles',default = 0) class Meta: verbose_name = 'Rushing Statistics' verbose_name_plural = 'Rushing Statistics' def yards_per_carry(self): if self.rushing_carries == 0: return 0 else: return (float(self.rushing_total_yards) / float (self.rushing_carries)) Also for rushing, receiving, punting. etc etc Things you'll probably want to consider Seasons - a very small model class, but organizing things by a season becomes pretty tricky with out this. Rosters ( players per season ) Staff ( staff Per season ) Game category ie, pre-season, regular, playoff, championship. * keeping track of win/losses and ties for each game type. However, my set up track only 1 team which defined by a simply boolean check, and can be changed at any time. I have a thin layer that keeps track of win/losses in the league, but not the stats of the whole league. I have a separate league manager application which handles things on a league wide level. So this may not fit your purpose exactly. I have a trac site set up. If you are are interested in looking over the source shoot me an email esatterwhite -[at]- wi.rr.com. I'll set you up with a password. On Oct 7, 4:16 pm, Curt <curt.merr...@gmail.com> wrote: > Instead of including every player in the Game model, I would add a > field to the Player model to assign which team a player belongs to: > > class Team(models.Model): > name = models.CharField(max_length=60) > > class Player(models.Model): > surname = models.CharField(max_length=60) > lastname = models.CharField(max_length=60) > team = models.ForeignKey(Team) > > Now you can just specify the home_team and the away_team in the Game > model without having to add every player. > > For more functionality (like allowing players to switch teams) you > could use a through table that includes Player, Team, join_date and > leave_date -- The example in the docs is a good starting point for > that:http://docs.djangoproject.com/en/dev/topics/db/models/ > > I'm not sure how to answer a), but you can figure out b) because the > goal_scorer will be assigned to either home_players or away_players in > your original Game model, or the goal_scorer will be assigned to the > home_team or away_team in the modified models. > > On Oct 5, 7:21 am, "c!w" <wittwe...@gmail.com> wrote: > > > > > Hi, > > I trying to create a hockey database, based on Django. > > The heaviest part so long is to define the structure of my models. > > There I need some help. > > What I have so far.. > > > class Team(models.Model): > > name = models.CharField(max_length=60) > > > class Player(models.Model): > > surname = models.CharField(max_length=60) > > lastname = models.CharField(max_length=60) > > > class Game(models.Model): > > date_time = models.DateTimeField() > > home_team = models.ForeignKey(Team,related_name='home_team') > > away_team = models.ForeignKey(Team,related_name='away_team') > > home_players = models.ManyToManyField(Player,blank=True) > > away_players = models.ManyToManyField(Player,blank=True) > > > class GameGoal(models.Model): > > goal_scorer = models.ForeignKey(Player,blank=True,null=True) > > first_assist = models.ForeignKey(Player,blank=True,null=True) > > game = models.ForeignKey(Game) > > > So now I have the following problems with these models. > > > a) How can I limit the choice (in the admin page) of goal_scorer to > > Players, which are assigned to the Game? > > b) A GameGoal should be connected to the home_team or the away_team, > > how can I handle this? Add a foreignkey to Team and limit the choice > > to the both teams? > > c) Is there a better way, to define such a case? --~--~---------~--~----~------------~-------~--~----~ 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---