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.
views:
107answers:
3
+2
A:
The unique_together
attribute of the Meta
class of your model is what you are looking for : django docs
class Meta :
unique_together = (('poll', 'user_id'),)
sebpiq
2010-05-21 09:52:39
You'll need a comma between the two close parentheses I think `unique_together = (('poll', 'user_id'),)`.
Dominic Rodger
2010-05-21 11:23:44
Oh yeah ... thanks
sebpiq
2010-05-21 12:48:24
A:
You want the unique_together
attribute:
http://www.djangoproject.com/documentation/0.96/model-api/#unique-together
scompt.com
2010-05-21 09:53:42