Django - 为 2 个或多个字段创建唯一的数据库约束 [英] Django - create a unique database constraint for 2 or more fields together

查看:23
本文介绍了Django - 为 2 个或多个字段创建唯一的数据库约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设,我想记录用户每天的投票选择.在这种情况下,我有一个名为 vote 的表,其中包含 pollchoiceuser-id 列.那么我怎样才能摆脱约束(可能在 django 模型中或任何可能的地方),polluser-id 对于任何条目都不应相同但相同用户可以一次为各种不同的民意调查投票,显然不同的用户可以为同一个民意调查投票.我希望我很清楚.

解决方案

unique_together 可能正是您要找的.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆