[Django]-Formset is valid but form has no attribute cleaned_data!

6👍

formset_factory returns a form iterator, i.e. essentially a list of forms, it is not a form itself. cleaned_data is only available on the form, so you have to iterate over formset2:

for form in formset2:
    form.cleaned_data # Here I am!

0👍

If someone need, here are the modifications I’ve done in the view:

 ClinicalSet = formset_factory(Clinical, extra=tamanh)

 #size of this list will determine number of forms
 wrongs = iter(wrong)
 formset = ClinicalSet(request.POST)
 dic = {}
 if formset.is_valid():
     if len(request.POST.keys()) == 0:
         return render_to_response('valid.html',  
                                        {
                                            'formset': formset,
                                            'wrongs': wrongs })
     else:
         #+ 2 because of TOTAL_FORMS and MAX_NUM_FORMS
         if len(request.POST.keys()) != len(wrongs) + 2:
             msg = "You have to select something in all forms!!"
             return render_to_response('valid.html', 
                                            {
                                                'formset': formset,
                                                'wrongs': wrongs,
                                                'msg': msg })
         else:
             for n in range(tamanh):
                 #result post into a dictionary, since cleaned_data doesn't work
                 dic[wrongs.next()] = request.POST['form-' + str(n) + '-cliform_name']
                 return HttpResponseRedirect(reverse('valid2', args=[word]))
    else:
        formset = ClinicalSet()
    return render_to_response('valid.html', 
                                {
                                    'formset': formset,
                                    'wrongs': wrongs,
                                    'msg': msg
                                    })  

Leave a comment