Django 的复合/复合主/唯一键
如何使用 Django 创建具有复合(复合)主/唯一键的模型(以及表)?
How can you create models (and thus tables) with a compound (composite) primary/unique key using Django?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Django 不支持复合主键。您可以使用
Meta 创建单个复合唯一键。 unique_together
。Django does not support compound primary keys. You can create a single compound unique key with
Meta.unique_together
.如果您只想将唯一的混合字段放在一起,请使用下面的代码:
但是如果您希望将唯一的混合字段放在一起并且其中一列是主要的,请为模型列设置
primary
参数,类似于以下代码:if you want only unique mixed fields together use belowcode:
But if you want unique together and one of column be primary, set
primary
argument for model column, similar below code: