Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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..

like image 905
psoares Avatar asked Feb 02 '26 20:02

psoares


1 Answers

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!
like image 72
Chris Pratt Avatar answered Feb 04 '26 13:02

Chris Pratt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!