我有Persons 和Proposals。现在我想将Person's投票存储在Proposals. 为此,我将创建另一个提供“中间”表的类。目标是将 aPerson与 a联系起来Proposal并关联投票direction(upvote 或 downvote)。下面的代码:
class Persons(models.Model):
#code
class Proposal(models.Model):
#code
class Vote(models.Model):
"""A Person (who) votes up or down (direction) a Proposal (what)."""
who = models.ForeignKey(Person)
what = models.ForeignKey(Proposal)
direction = models.BooleanField() # true = upvote = vote in favor
我希望投票的 PrimaryKey 是(谁,什么)在一起。相反,Django 创建了一个默认AUTO-INCREMENTED id为 PrimaryKey 的表。如何将 ForeignKey(或一组 ForeignKey)设置为 PrimaryKey?