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"
Django ManyToMany Relationship Raises error?
-
05-07-2023 - |
Question
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
La solution
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow