当我尝试保存我的内联表单集时,它只返回一个空列表,并且数据库中不会反映任何更改。我尝试在没有选项的情况下执行此操作并提交=False,但它们都有相同的结果。我知道有数据,因为我将表单集打印为表格,并且我知道它是有效的,因为属性 is_valid(( 方法返回 true。这是代码:
def edit(request):
if request.method == 'POST':
print(request.POST)
form = TombstoneForm(request.POST)
print(form.is_valid())
t = form.save(commit=False)
t.edit_date = datetime.now()
t.user_editor = request.user
t.save()
print(t)
formset_construct = inlineformset_factory(Tombstone, Tank, form=TombstoneForm)
formset = formset_construct(request.POST)
print("Passed the inline formset")
print(formset.as_table())
print(formset.is_valid())
l = formset.save()
print(l)
return render(request, 'main.html')
所以我相信我已经找到了问题的根源和解决方法。此方法中的 BaseModelFormSet 类中出现问题:
def save_existing_objects(self, commit=True):
self.changed_objects = []
self.deleted_objects = []
if not self.initial_forms:
return []
saved_instances = []
forms_to_delete = self.deleted_forms
for form in self.initial_forms:
obj = form.instance
# If the pk is None, it means either:
# 1. The object is an unexpected empty model, created by invalid
# POST data such as an object outside the formset's queryset.
# 2. The object was already deleted from the database.
if obj.pk is None:
continue
if form in forms_to_delete:
self.deleted_objects.append(obj)
self.delete_existing(obj, commit=commit)
elif form.has_changed():
self.changed_objects.append((obj, form.changed_data))
saved_instances.append(self.save_existing(form, obj, commit=commit))
if not commit:
self.saved_forms.append(form)
return saved_instances
问题发生在:
if obj.pk is None:
continue
它总是点击继续的地方,所以我的解决方案只是保存单个表单数据而不是表单集数据:
for form in formset:
val = form.save(commit=False)
val.Vessel = t
val.save()
为了帮助其他人,他们在 formset 保存返回空列表时遇到类似的问题,并且 SO 上没有太多其他内容:
根据Django文档:
调用 save(( 后,模型表单集将具有三个包含表单集更改的新属性:
模型。BaseModelFormSet.changed_objects
型号。BaseModelFormSet.deleted_objects
型号。BaseModelFormSet.new_objects
new_objects
只是新obj
的列表,即
[obj1, obj2]
changed_objects
是元组列表,即
[
(obj1, [list of field names changed]),
(obj2, [list of field names changed]),
...
]
我遇到了一个问题,deleted_objects总是[]
.
首先,确保表单中有必填的隐藏字段:
{% for form in formset %}
{{ form.id }} # <- have you got this?
{{ form.DELETE }}
...
{{ form.foo }}
{% endfor %}
其次,我有一个好主意,即当用户选中删除复选框时禁用所有字段。禁用的项目不会发布,因此要删除的项目将不是PK,正如OP上面提到的,如果 obj.pk 为空白,则不会将其添加到(在这种情况下.deleted_objects
(列表中。解决方案是使用只读,在JavaSript中使用.readOnly = true
,在css:read-only
中使用。