内联表单集是否在保存时返回空列表? [英] Inline formset returns empty list on save?
问题描述
当我尝试保存嵌入式表单集时,它仅返回一个空列表,并且数据库中未反映任何更改。我试过没有选项和commit = False,但它们都有相同的结果。我知道有数据是因为我将表单集打印为表格,并且我知道它是有效的,因为属性is_valid()方法返回true。以下是代码:
When I try to save my inline formset it just returns an empty list and no changes are reflected in the database. I have tried doing it with no option and commit=False but they both have the same result. I know there is data because I printed the formset as a table, and I know it is valid because the property is_valid() method returns true. Here is the code:
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类中出现问题:
So I believe I have found the source of my problem and a workaround. The problem was occuring in the BaseModelFormSet class in this method:
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
总是命中的地方,所以我的解决方案只是保存单个表单数据而不是表单集数据:
Where it was always hitting continue, so my solution has just been to save the individual form data instead of the formset data:
for form in formset:
val = form.save(commit=False)
val.Vessel = t
val.save()
这篇关于内联表单集是否在保存时返回空列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!