0

我有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?

4

3 回答 3

3

Django ORM 不支持复合主键:https ://code.djangoproject.com/wiki/MultipleColumnPrimaryKeys

于 2013-03-15T19:28:33.477 回答
3

我也有类似的问题,我能够通过使用 unique_together 元(在你的情况下在类 Vote 下)来解决它。阅读更多关于 unique_together 的信息

https://docs.djangoproject.com/en/dev/ref/models/options/

于 2013-09-06T05:00:34.073 回答
2

不幸的是,您可能无法使用SQLAlchemy,因为标准 ORM 无法做到这一点。

于 2013-03-15T19:36:02.643 回答