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

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

问题描述

假设,我想每天记录用户的民意测验选择。在这种情况下,我有一个名为 vote 的表,该表具有列 poll choices user-id 。所以我怎么能排除民意测验用户ID 的约束(也许在Django模型中,或者在任何可能的情况下)任何条目都不应相同,但就像同一用户可以一次为各种不同的投票投票一样,显然,各个用户可以为同一项投票投票。我希望我清楚。

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 可能就是您想要的。

这篇关于Django-同时为2个或更多字段创建唯一的数据库约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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