我正在使用 jQuery.formsets 在 Django 模板下动态创建更多表单。问题是我将重复的值输入到数据库中。
据我所知,jQuery.formsets 生成的 ID 是正确的,因为当我在视图下打印 request.POST 时,它会显示正确的值。
有问题的值是“观察者一/二/三”,在单击提交之前具有以下 ID:
- id_form-0-observed_by_0
- form-1-id_form-0-observed_by_0
- form-2-id_form-0-observed_by_0
请求.POST:
<QueryDict: {u'form-1-form-INITIAL_FORMS': [u''], u'form-1-form-MAX_NUM_FORMS': [u''], u'commune': [u'Tartu'], u'additional_workgroups_view_0': [u''], u'form-0-observed_by_1': [u'3'], u'form-0-observed_by_0': [u'Observer ONE'], u'sample': [u''], u'remarks': [u''], u'access_edit': [u'private'], u'district': [u'Tartu'], u'country_1': [u'EE'], u'access_view': [u'public'], u'form-2-form-MAX_NUM_FORMS': [u''], u'substrate_1': [u''], u'substrate_0': [u''], u'geom': [u''], u'exact_date_and_time': [u''], u'latitude': [u'25.36698180'], u'csrfmiddlewaretoken': [u'AJTXFtEYQbX0pdBdvSe53443HKHN5dvg'], u'method': [u'MAP'], u'coordinate_uncertainty_in_meters': [u'200'], u'form-1-form-TOTAL_FORMS': [u''], u'elevation': [u''], u'country_0': [u'Estonia'], u'timespan_begin': [u'25-03-2013 11:08'], u'locality_text': [u'Ravila 19'], u'additional_users_view_0': [u''], u'timespan_end': [u''], u'form-TOTAL_FORMS': [u'3'], u'form-INITIAL_FORMS': [u'0'], u'form-2-form-INITIAL_FORMS': [u''], u'taxon_node_0': [u'Fungi'], u'taxon_node_1': [u'5'], u'district_ehak': [u''], u'form-2-observed_by_1': [u'3'], u'form-2-observed_by_0': [u'Observer THREE'], u'form-1-observed_by_0': [u'Observer TWO'], u'form-1-observed_by_1': [u'1'], u'form-2-form-TOTAL_FORMS': [u''], u'commune_ehak': [u''], u'additional_users_edit_0': [u''], u'form-MAX_NUM_FORMS': [u''], u'study': [u'2'], u'longitude': [u'15.69302410'], u'additional_workgroups_edit_0': [u''], u'remarks_sample': [u''], u'kingdom_helper': [u'5']}>
但是,当我打印出表单集本身时,form0 和 form2 的值似乎是重复的。
<input type="hidden" name="form-TOTAL_FORMS" value="3" id="id_form-TOTAL_FORMS" />
<input type="hidden" name="form-INITIAL_FORMS" value="0" id="id_form-INITIAL_FORMS" />
<input type="hidden" name="form-MAX_NUM_FORMS" id="id_form-MAX_NUM_FORMS"/>
<tr>
<th>
<label for="id_form-0-observed_by_0"></label>
</th>
<td>
<input style="width: 200px" name="form-0-observed_by_0" required="True" value="Observer THREE" id="id_form-0-observed_by_0" data-selectable-type="text" data-selectable-allow-new="false" data-selectable-url="/selectable/agent-personlookup/" type="text" class="span12"/>
<input data-selectable-type="hidden" name="form-0-observed_by_1" id="id_form-0-observed_by_1" required="True" type="hidden" style="width: 200px" value="3" class="span12" />
</td>
</tr>
<tr>
<th>
<label for="id_form-1-observed_by_0"></label>
</th>
<td>
<input style="width: 200px" name="form-1-observed_by_0" required="True" value="Observer ONE" id="id_form-1-observed_by_0" data-selectable-type="text" data-selectable-allow-new="false" data-selectable-url="/selectable/agent-personlookup/" type="text" class="span12"/>
<input data-selectable-type="hidden" name="form-1-observed_by_1" id="id_form-1-observed_by_1" required="True" type="hidden" style="width: 200px" value="1" class="span12"/>
</td>
</tr>
<tr>
<th>
<label for="id_form-2-observed_by_0"></label>
</th>
<td>
<input style="width: 200px" name="form-2-observed_by_0" required="True" value="Observer THREE" id="id_form-2-observed_by_0" data-selectable-type="text" data-selectable-allow-new="false" data-selectable-url="/selectable/agent-personlookup/" type="text" class="span12"/>
<input data-selectable-type="hidden" name="form-2-observed_by_1" id="id_form-2-observed_by_1" required="True" type="hidden" style="width: 200px" value="3" class="span12"/>
</td>
</tr>
看法:
def observation_add(request):
if request.method == 'POST':
print 'REQUEST POST START'
print request.POST
print 'REQUEST POST END'
ObservedByFormset = formset_factory(ObservedByForm)
observed_by_formset = ObservedByFormset(request.POST)
print 'DEBUG: FORM BEFORE VALID:', observed_by_formset