I am new to Django and need to create a form for team scheduling. The form reads the voter ID through a checkbox, and then add the object of voter ID to the particular Choice. Choice has a field named

voters = ManyToMany(Voter)

Now, what I do is:

v = Voter.objects.filter(voter_name__icontains=request.POST.get('voter'))
selected_choice.voter.add(v[0])

But this won't prevent them from voting twice. Does add() give an silenced error if the voter selected is already in the Choice?

Many Thanks

有帮助吗?

解决方案

You can add twice to ManyToMany. Look at the doc https://docs.djangoproject.com/en/1.6/topics/db/examples/many_to_many/#many-to-many-relationships, search for "Adding a second time is OK"

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top