Zagorodniy Olexiy
Zagorodniy Olexiy

Reputation: 2212

Django form: object has no attribute

I have the form that looks like this:

class PeopleForm(forms.Form):
  adult = forms.IntegerField(min_value=1, initial=1)
  children = forms.IntegerField(min_value=0, required = False)

The quantity of this form depend on value of other field in other form. So I create formset for this form in the view. In the final result, I need to get to the field adult of the forms PeopleForm and create the string that should look like this Adult=2, Adult=3.

So my view looks like this:

PeopleFormSet = formset_factory(PeopleForm, extra = 1, max_num = 15)
people_formset = PeopleFormSet(request.POST, prefix='people')
  if people_formset.is_valid():
    adults = ''
    for i in people_formset:
      adults = i.adult

But I've got an error 'PeopleForm' object has no attribute 'adult'. I don't understand how to get to the field adult of people_formset.

Upvotes: 0

Views: 1275

Answers (1)

Alasdair
Alasdair

Reputation: 309109

You can access the value from the form's cleaned_data.

for form in people_formset:
    adults = form.cleaned_data['adult']

Upvotes: 3

Related Questions