Django表单:对象没有属性

时间:2016-05-18 10:11:26

标签: python django

我的表单看起来像这样:

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

此表单的数量取决于其他形式的其他字段的值。所以我在视图中为这个表单创建了formset。在最终结果中,我需要访问表单adult的字段PeopleForm并创建应该看起来像Adult=2, Adult=3的字符串。

所以我的观点如下:

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

但我有一个错误'PeopleForm' object has no attribute 'adult'。我不明白如何访问adult的{​​{1}}字段。

1 个答案:

答案 0 :(得分:2)

您可以访问表单cleaned_data

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