0
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)

From the above structure, I'd like 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.

I was thinking of using inline formsets for this, and had started on some code

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})
<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>

However, when I go to the form page, it's always blank. What could be the issue? I'd really appreciate any help on this.

1
Contributor
1
Reply
2
Views
5 Years
Discussion Span
Last Post by ste_a47
0

The formset method hasn't worked out so well for me, so I just pulled the list of question depending on the selected group and displayed this in the template with Yes/No radio buttons alongside each, then in the view, I get the submitted questions and radio button values to use for the InspectionQuestion instance

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.