我有一个使用默认{{ formset }}
呈现的formset。在提交表单时,页面会回复自己。
问题是,在创建新实例时,包含ID的字段仍为空白,如<input id="id_form-0-id" name="form-0-id" type="hidden">
。因此,如果我在表单返回后重新提交表单,它最终会创建新实例,而不是更新新创建的实例。如果我刷新页面,则字段包含ids <input id="id_form-0-id" name="form-0-id" type="hidden" value="18">
。
这是我的控制器功能:
def main_categories (request):
dict = {}
FormSet = modelformset_factory (MainCategory, formset = MainCategoryFormSet, fields = ['name'], extra = 1, can_delete = True)
if request.method == 'POST':
formset = FormSet (request.POST)
if formset.is_valid ():
with transaction.atomic ():
formset.save ()
else:
formset = FormSet ()
dict ['formset'] = formset
return render (request, 'equipment/admin/main_categories.html', dict)