Django - 为 2 个或多个字段创建唯一的数据库约束 [英] Django - create a unique database constraint for 2 or more fields together
问题描述
假设,我想记录用户每天的投票选择.在这种情况下,我有一个名为 vote
的表,其中包含 poll
、 choice
和 user-id
列.那么我怎样才能摆脱约束(可能在 django 模型中或任何可能的地方),poll
和 user-id
对于任何条目都不应相同但相同用户可以一次为各种不同的民意调查投票,显然不同的用户可以为同一个民意调查投票.我希望我很清楚.
unique_together 可能正是您要找的.p>
Suppose, I want to record say poll choices by users everyday. In this case, i have a table named vote
which has columns poll
, choice
and user-id
. So how can i out the constraint (maybe in the django models or wherever possible) that poll
and user-id
both should not be the same for any entry but like the same user can vote for various different polls once and obviously various users can vote for the same poll. I hope I am clear.
unique_together may be what you are looking for.
这篇关于Django - 为 2 个或多个字段创建唯一的数据库约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!