Django circular model reference

前端 未结 8 1338
长发绾君心
长发绾君心 2020-11-29 07:42

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,

8条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-29 08:15

    You could use the full application label in the foreign key to the model not yet defined, and use related_name to avoid name conflict:

    class Team(models.Model):
        captain = models.ForeignKey('myapp.Player', related_name="team_captain")
    
    class Player(models.Model):
        team = models.ForeignKey(Team)
    

提交回复
热议问题