Django League Table

October 24th, 2008 by andylockran Leave a reply »

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.

Advertisement