I’m trying to write a league table for some sport results using django. At the moment my model looks pretty basic:
class Scores(models.Model):
matchnum = models.AutoField(primary_key=True)
homeuser = models.ForeignKey(User, related_name="hmuser")
hometeam = models.ForeignKey('teams', related_name="hmteam")
homescore = models.IntegerField()
wayuser = models.ForeignKey(User)
awayteam = models.ForeignKey('teams')
awayscore = models.IntegerField()
pub_date = models.DateTimeField(auto_now_add=True, editable=False)
def __unicode__(self):
id = str(self.matchnum)
return id
class Meta:
verbose_name_plural="Scores"
class Teams(models.Model):
teamname = models.CharField(max_length=40)
def __unicode__(self):
return self.teamname
class Meta:
verbose_name_plural = "Teams"
However, with the information in this simple model - I should be able to generate a league table, with the following fields:
| Team | Played # | Won | Drawn | Lost | Goals For | Goals Against | Goal Difference | Points |
|---|
So how to put this into a Django App? Well I need some advice, as my original method was to edit the save() function of the model to input fields into a new model - however, I should be editing things at the form level. I’d also like a ‘generate league’ method, rather than a method where a league is generated at the end of each score addition. The main advantages of this approach is that I can:
- Generate the League from existing score data.
- Future Users can generate the League from their data.
- Leagues can be generated at the end of each ‘gameweek’ - rather than just at the end of each game.
Django is a fantastic python framework that I’m immediately getting to grips with - but as this kind of application (simple as it is) - is one that’s scalable and may be useful to many other users.. I want to get it right from the start.
I’ll be tagging the posts with ‘djangoleague’ if you want to keep up with development - and if anyone wants to use the project/help me out with it - please post in the comments and I’ll get back to you.
Would this be a time to explore the signals on save? Haven’t used it myself, but would you be able to update your stats table by sending a signal when someone updates/adds a score set?
I did look down this avenue before, as with a previous model I used this method to generate one of the fields in the model.. and it worked well.
However, I was reliably informed by Magus- in #django on IRC, that this is definately not the right method to be using. The other issue is that it does not fulfil my criteria of being able to ‘generate’ the league at a predefined time.. either on a button press or at a specific time. (end of gameweek .etc)
http://collingrady.wordpress.com/2008/02/18/editing-multiple-objects-in-django-with-newforms/
I am nearly done with a league system that I am building for a client. Current features are divisions (or conferences), teams, logos, players, player icons, games, gamedays, game stats, team stats, and player stats per game, per season, and career. The current build is for basketball, but I think I may be able to abstract out the stats and allow league managers to create their own stats fields.