我有一个父窗体和几个使用内联窗体集的子窗体。它工作得很好。
根据父窗体中的值,我需要检查是否提交了正确数量的子窗体。
我知道我可以在覆盖BaseInlineFormSet时使用self.instance.FOO访问父表单,但我找不到一种方法来确定实际提交了多少表单以及其中包含重要的数据。
有人知道怎么做吗?
非常感谢
发布于 2018-12-15 00:10:58
在formset clean方法中,self.cleaned_data将是一个字典列表-每个表单一个字典。所以你可以这样做:
class MyInlineFormSet(formsets.InlineFormSet):
def clean(self):
if len(self.cleaned_data) != self.instance.my_value:
raise forms.ValidationError('wrong number of forms')发布于 2018-12-19 20:55:30
谢谢你,大牛,为我指明了正确的方向。通过覆盖baseinlineformset并在clean方法中执行以下操作,我实现了所需的功能:
# submitted form counter
i = 0
for form in self.forms:
cleaned_data = form.cleaned_data
if cleaned_data:
# discounts forms marked for deletion
if not self._should_delete_form(form):
i = i + 1
if self.instance.ownership.type == 'Joint' and i < 2:
raise ValidationError(
"You must enter both clients when specifying joint "
"ownership."
)等。
我还发现,我还需要从表单计数中删除在同一帖子中删除的任何表单。
https://stackoverflow.com/questions/53782457
复制相似问题