I have a form with three parts; the top part contains information about decisions. This should be a single row of three fields - which it is and this works fine.
I then have two other sections; both having a many-to-1 relationship with the model that the first part is based upon.
The top part displays details on outcomes; 2, maybe three fields.
The lower part displays details on options; again 2, maybe three, fields.
I am creating a blank form that allows me to create a new decision and should begin with 2 blank rows of fields for outcomes and two blank rows for options.
I have almost all of this in place, yet the outcomes will only show a single row of fields.
The template file contains the following…
{% block content %}
<form method="post">
{% csrf_token %}
<h1>New Decision</h1>
{{ form.as_table }} {# Renders fields from DecisionForm #}
<h4>Objectives</h4>
{{ outcome_formset.management_form }}
<div id="outcome-formset-section">
<table>
{% for form in outcome_formset %}
<tr>
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% for field in form.visible_fields %}
<td>
{{ field.label_tag }}
{{ field }}
</td>
{% endfor %}
</tr>
{% endfor %}
</table>
</div>
<button type="button" id="add-outcome">Add Outcome</button>
<h4>Options</h4>
<div id="formset-section">
<table>
{% for form in formset %}
<tr>
{% for field in form.visible_fields %}
<td>
{{ field.label_tag }}
{{ field }}
</td>
{% endfor %}
</tr>
{% endfor %}
</table>
</div>
<button type="button" id="add-option">Add Option</button>
<button type="submit">Save</button>
</form>
The buttons listed work; well, the add buttons do. When I click on these they display a new row of fields for both the options part and for the outcomes part.
In my views.py section I have this:
class DecisionCreateView(CreateView):
model = Decision_Model
form_class = DecisionForm
template_name = 'template.html'
success_url = reverse_lazy('success_url_name')
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
organisation_slug = self.kwargs['organisation_slug']
organisation = Organisation.objects.get(slug=organisation_slug)
context['organisation_slug'] = organisation_slug
context['user_org'] = organisation.organisationuser_set.all()
if self.request.POST:
context['formset'] = inlineformset_factory(Decision_Model, Option_Model, form = OptionForm, extra = 2)(self.request.POST)
context['outcome_formset'] = OutcomeFormSet(self.request.POST, prefix='outcome')
else:
context['formset'] = inlineformset_factory(Decision_Model, Option_Model, form = OptionForm, extra = 2)()
context['outcome_formset'] = OutcomeFormSet(prefix='outcome', queryset=Outcome_Model.objects.none())
return context
def form_valid(self, form):
context = self.get_context_data()
formset = context['formset']
outcome_formset = context['outcome_formset']
if formset.is_valid() and outcome_formset.is_valid():
self.object = form.save()
formset.instance = self.object
formset.save()
outcome_formset.instance = self.object
outcome_formset.save()
return redirect(self.get_success_url())
else:
return self.render_to_response(self.get_context_data(form = form))
This view works fine for the options but the outcomes only shows a single row of fields.
Anyone any idea why this is so?