[Answer]-Repeated modelform not validating in modelformset โ€“ django

1๐Ÿ‘

โœ…

You are using model forms, so you should use modelformset_factory instead of formset_factory. You can create the formset class outside of the create view. Then, you need to instantiate the formset in the GET and POST branches of your view.

Putting it together, you have the following (untested, so there might be some typos!)

WorkOrderFormSet = formset_factory(WorkOrder_Form, extra=1)

def create(request):
    if request.method == 'POST':
        post_data = request.POST.copy()
        formset = WorkOrderFormSet(data=post_data, queryset=WorkOrder.objects.none())
        if formset.is_valid():
            formset.save()
        else:
            print 'INVALID'
        return HttpResponseRedirect('/Shelling/') 
    else:
        formset = WorkOrderFormSet(queryset=WorkOrder.objects.none())
        return render_to_response('create.html',{'formset':formset}, context_instance=RequestContext(request))

And in the template:

{% for form in formset %}
    {{ form.as_ul }}
{% endfor %}
๐Ÿ‘คAlasdair

Leave a comment