我创建了一个formset,我知道表单数据是作为字典返回的。我以为我可以在formset中循环遍历表单并将每个字典存储在另一个字典中。这似乎不起作用,因为我只能存储最新的表单(在我的模板中我可以动态添加和删除表单)。我试过的是:
def index(request):
data = {}
data['education_data'] = {}
EducationFormSet = formset_factory(EducationForm)
if request.method == "POST":
persoform = PersonalForm(request.POST)
education_formset = EducationFormSet(request.POST)
if persoform.is_valid() and education_formset.is_valid():
data['personal_data'] = persoform.cleaned_data
entry = 1
en = 'entry_%d' % entry
for form in education_formset:
data['education_data'][en] = form.cleaned_data
entry = entry + 1
print(data)
我想要的输出是:
data = {' personal_data':{inputs},' education_data':{' entry_1':{inputs},...' entry_n& #39; {输入}}}
编辑:
问题在于我尝试将键添加到" Education_Data"键。以下作品:
for form in education_formset:
data['education_data']['entry_%d' % entry] = form.cleaned_data
[...]