psoares
psoares

Reputation: 4883

formset is valid but form has no attribute cleaned_data!

Ok, so I have a formset that is valid. But gives me a error that that form has no attribute cleaned_data..

Honestly I have absolutely no clue what's happening..
I tried my code on terminal and it returned a empty dictionary.. without errors..

forms:

class Clinical(forms.Form):
    _names = list(ClinicalForm.objects.values_list('form_id', 'form_name'))
    _names.append(("New", u'Nova entrada'))

    cliform_name = forms.ChoiceField(widget=RadioSelect(), choices=_names, label
         ="", required=True)

views:

ClinicalSet = formset_factory(Clinical, extra=2)

formset2 = ClinicalSet(request.POST)
if formset2.is_valid():
    choice1 = formset2.cleaned_data
    return render_to_response('valid_test.html', 
                                {
                                    'formset2': formset2,
                                    'wrongs1': wrongs1,
                                    'choice1': choice1 
                                    })
else:
    formset2 = ClinicalSet()
return render_to_response('valid_test.html', 
                            {
                                'formset2': formset2,
                                'wrongs1': wrongs1,
                                })    

template:

<form method="post" action="">
<div>
{{ formset2.management_form }}
    {% for form in formset2.forms %}
        {{ form }}
    {% endfor %} 
    <input type="submit" value="save" />
</div>

If I comment the line where the cleaned_data is called (choice1), I don't receive any error and I'm able to see the forms..
If I select some options and uncomment this line, it works..

I have a similar formset : both forms in formset need to be selected and this one works..

the form that works is the first formset (linked above). The post parameters:

form-0-pres_name    1
form-1-pres_name    2
form-INITIAL_FORMS  0
form-TOTAL_FORMS    2

the user select one option in each form and he's redirect to another view (this one - formset2).

Any help is more than welcome..

Upvotes: 3

Views: 7983

Answers (2)

psoares
psoares

Reputation: 4883

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

Upvotes: 0

Chris Pratt
Chris Pratt

Reputation: 239460

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!

Upvotes: 6

Related Questions