I’m starting to work on a small soccer league management website (mostly for learning purposes) and can’t wrap my mind around a Django models relationship. For simplicity, let’s say I have 2 types of objects – Player and Team. Naturally, a player belongs to one team so that’s a ForeignKey(Team) in the Player model.
So I go:
class Team(models.Model):
name = models.CharField()
class Player(models.Model):
name = models.CharField()
team = models.ForeignKey(Team)
Then I want each team to have a captain which would be one of the players so that would be a ForeignKey(Player) in the Team model. But that would create a circular dependency.
Granted my Django experience is limited, but it seems like a simple problem, though I can’t figure out what I’m doing wrong conceptually.
Here is another way to tackle this problem.
Instead of creating a circular dependency, I created an additional table that stores the relationship between players and teams. So in the end it looks like this:
It might be slightly less efficient in terms of storage than the suggested workaround, but it avoids the circular dependency and keeps the DB structure clean and clear.
Comments are welcome.