我的表单看起来像这样:
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}}字段。
答案 0 :(得分:2)
您可以访问表单cleaned_data
。
for form in people_formset:
adults = form.cleaned_data['adult']