私はDjangoを使ってサッカーリーグの管理ウェブサイトに取り組んでいます。なかでも、私は次のモデルがあります:unique_together on self
class PlayerRole(Model):
player = OneToOneField(Player, primary_key=True)
team = ForeignKey(Team, null=True)
role = CharField(max_length=20, choices=PLAYER_ROLES)
#role can be either "Captain" or "Regular" for a certain team
class Meta:
unique_together = ("player", "team")
class Season(Model):
start_date = DateTimeField(null=True, blank=True)
end_date = DateTimeField(null=True, blank=True)
label = CharField(max_length=20)
team = ManyToManyField(Team, null=True, blank=True)
player_role = ManyToManyField(PlayerRole, null=True, blank=True)
アイデアは、プレイヤーが特定のシーズン中に持っていたどのような役割を追跡することです。シーズン中にプレーヤーが1つの役割しか持たないように、複数のシーズンに渡ってプレイヤーが複数の役割を果たすことができるように、今シーズンとplayer_roleに対してunique_together
という種類の制約を適用したいと思います。明らかに、unique_together ('season', 'player_role')
とunique_together(self, 'player_role')
は機能しません(これらは私の頭のすぐ上にあります)。
私の質問は次のとおりです。記述された制約はどのように実装されますか?
あなたはself' 'と制約を望んでいた場合 - それは、そのフィールドであるとして、あなたはフィールドとして、主キーを使用したいと思いますそれぞれのシーズンをユニークにします( 'id'、 'player_role')。しかし、あなたの場合、 'player_role'はM2Mフィールドであり、私はdjangoがunique_together m2mでどのように動作するのかよくわかりません...これがコメント/部分的な答えである理由です。 :) GL! –
Djangoが文句を言う: 'tm.season:" unique_together "はplayer_roleを指します。 ManyToManyFieldsはunique_togetherでサポートされていません。 ' 私はこれの実装を再考する必要がありますね。ありがとう、ゆうじ。 – exfizik