Django circular model reference

前端 未结 8 1319
长发绾君心
长发绾君心 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:00

    Neither of the answers here are really that great - creating circular references is never a good idea. Imagine if your database crashed and you had to create it from scratch - how would you create player before team is created, and vice versa? Look a question here: ForeignKey field with primary relationship one I asked a few days ago. Put a Boolean on Player that specifies the captain, and put some pre-save hooks that validate every team must have one-and-only-one captain.

提交回复
热议问题