I've setup my classes as below:
class InspectionGroup(models.Model):
group = models.CharField(max_length=50)
class InspectionItem(models.Model):
group = models.ForeignKey(InspectionGroup)
item = models.CharField(max_length=50)
class InspectionQuestion(models.Model):
item = models.ForeignKey(InspectionItem)
question = models.CharField(max_length=200)
question_pass = models.BooleanField()
class InspectionResult(models.Model):
question = models.ForeignKey(InspectionQuestion)
vehicle = models.ForeignKey(Vehicle)
result = models.BooleanField()
submitted_by = models.ForeignKey(User, editable=False)
date_time_submitted = models.DateTimeField(default=datetime.today,
editable=False)
What I want to do is to have the user choose an inspection group, then from that group they'll be presented with all the questions under that group, with their item headers being used for grouping in the template. They'll then answer the questions and post the results.
My problem is getting this done. I had started on some code, but it just doesn't seem right.
forms.py
QuestionFormSet = inlineformset_factory(InspectionItem, InspectionQuestion,
form=QuestionForm, extra=0, can_delete=False)
class BaseInspectionFormset(BaseInlineFormSet):
def add_fields(self, form, index):
# allow the super class to create the fields as usual
super(BaseInspectionFormset, self).add_fields(form, index)
# created the nested formset
try:
instance = self.get_queryset()[index]
pk_value = instance.pk
except IndexError:
instance=None
pk_value = hash(form.prefix)
# store the formset in the .nested property
form.nested = [QuestionFormSet (instance = instance,
prefix = 'INSPECTIONQUESTIONS_%s' % pk_value)]
InspectionFormset = inlineformset_factory(InspectionGroup, InspectionItem,
formset=BaseInspectionFormset, extra=0, can_delete=False)
view.py
def vehicle_inspection(request, stock_number, id):
vehicle = get_object_or_404(Vehicle, stock_number=stock_number)
group = get_object_or_404(InspectionGroup, pk=id)
if request.method == 'POST':
formset = InspectionFormset(request.POST, instance=group)
if formset.is_valid():
results = formset.save_all()
return HttpResponseRedirect(reverse('inspectionRecord',
kwargs={'stock_number': stock_number}))
else:
formset = InspectionFormset(instance=group)
return render_to_response('vehicles-inspection-form.html', {
'formset': formset})
Could someone confirm if this idea will work or what I'll need to do to have the functionality I want? Currently I'm getting this error:
[u'ManagementForm data is missing or has been tampered with']
Update 1 I've managed to resolve the above error by following this link from readthedocs
However, now currently, it seems the formset is empty since the template shows just the submit button.
<form action="." method="POST" id="inspection_form">
{% csrf_token %}
<div id="form">
{{ formset.management_form }}
{% for form in formset.forms %}
{{ form }}
{% if form.nested %}
{% for formset in form.nested %}
{{ formset.as_table }}
{% endfor %}
{% endif %}
{% endfor %}
</div>
<div class="form_buttons">
<button type="submit" name="confirm" id="confirm">
<span>Submit</span>
</button>
</div>
</form>
What can be the issue now?
the documentation seems to have the answer: https://docs.djangoproject.com/en/dev/topics/forms/formsets/#understanding-the-managementform
EDIT: for form in formset
instead of formset.forms
?
and {{ form.as_table }}
instead of {{ form }}
.