我有一个表格,其中模型和外键模式表示为内联表单集。我正在节省订购的表单集的时间。事实上,每次我尝试删除一个时,它都会成倍增加。
在forms.py中:
class PublicationForm(ModelForm):
class Meta:
model = Publication
fields = ['title']
SectionFormSet = inlineformset_factory(Publication, Section, can_delete=True, can_order=True, extra=2)
在views.py中:
if publication_form.is_valid():
pub = publication_form.save(commit=False)
section_formset = SectionFormSet(request.POST, instance=pub, prefix='section')
if section_formset.is_valid():
pub.save()
for s in section_formset.ordered_forms:
s.instance.order = s.cleaned_data['ORDER']
s.save()
我一直在寻找,但什么也没找到。有人有解决方案吗?
谢谢!!