I'm trying to create models that represent a swiss tournament, with multiple rounds. Each round everyone will be paired up with another player, except in the case where there is an odd player out, when one player will get a bye.
I need to keep track of the outcome of each pairing; i.e., which player won. Also, I'd like to be able to efficiently search later for all players who have ever played against a given player.
The obvious stuff:
class Tournament(models.Model):
name = models.CharField(max_length=80)
class Player(models.Model):
name = models.CharField(max_length=80)
At first I was planning to have a "TournamentPairing" class that looked something like this:
class TournamentPairing(models.Model):
tournament = models.ForeignKey(Tournament)
round = models.IntegerKey()
player1 = models.ForeignKey(Player)
player2 = models.ForeignKey(Player, null = True) # In case of a bye, this is None.
outcome = models.CharField(max_length=1) # player1 wins, loses, bye, or tie
But that feels kind of hacky, especially the part about player2 being None sometimes. Also, I don't think it facilitates searching very well (since the player we're looking for could be in the player1 or player2 slot).
Is there a better way? I suspect my django noob-ness is holding me back from finding the right solution here.