I am trying to save a form with an m2m intermediate table. I get this error when I implement a multiplechoicefield form:
Cannot assign "[Option: Option1]": "StateOption.partoption" must be a "Option" instance.
However, if I comment that out the form will save but can only save 1 option at a time. I would like to be able to have a checklist and have the user select multiple options.
I am using this: Django m2m form save " through " table as a reference but must admit I don't understand it too well, especially the for loop part.
My question is: How do I save the form? I understand why I am getting the instance error but I'm not sure how to solve it. Also, the for loop below is probably wrong. Any advice on how to fix the for loop would be appreciated. It seems like that will allow me to save a multiple choice selection.
models.py
class Disease(models.Model):
disease = models.CharField(max_length=300, verbose_name="disease")
class Option(models.Model):
relevantdisease = models.ForeignKey(Disease, verbose_name="disease")
option = models.CharField(max_length=300, verbose_name="treatment Options")
class State(models.Model):
state = models.CharField(max_length=300, verbose_name='state')
relevantdisease = models.ForeignKey(Disease, verbose_name="disease")
relevantoption = models.ManyToManyField(Option, through='StateOption')
class StateOption(models.Model):
parttstate = models.ForeignKey(State)
partoption = models.ForeignKey(Option)
relevantoutcome = models.ManyToManyField(Outcome, through='StateOptionOutcome')
forms.py
class StateOptionForm(forms.ModelForm):
partoption = forms.ModelMultipleChoiceField(queryset=Option.objects.all(), required=True, widget=forms.CheckboxSelectMultiple)
#if I comment the line above out, then my selection will save but only 1 at at time. There are multiple options for a 'state' and I'd like to capture it all at once
class Meta:
model = StateOption
exclude = ['parttstate', 'relevantoutcome']
views.py
def diseasestateoption(request, disease_id, state_id):
state = get_object_or_404(State, pk=state_id)
disease = get_object_or_404(Disease, pk=disease_id)
if request.method == "POST":
form = StateOptionForm(request.POST, request.FILES)
if form.is_valid():
profile = form.save(commit=False)
profile.user = request.user
profile.save()
for state in request.POST.getlist('relevantoption'): #don't really understand this part and I'm probably not implementing this right
option = StateOption.objects.create(partstate=state, partoption=profile)
return HttpResponseRedirect(reverse('state', kwargs={'disease_id':disease_id}))
else:
form = StateOptionForm()
template
<form class="option_form" action="{% url "diseasestateoption" disease.pk state.pk %}" method="post">{% csrf_token %}
{{ disease }}
{{ state }}
{{option}}
I've posted a similar question here before but did not get the answer. I'm new to django and stackoverflow, so any help would be appreciated.
Edit
I am able to implement this with no intermediate tables and using a save_m2m(), however, I will need an intermediate table for the next part of my program...if I can solve this portion first.