Django InlineFormSet验证 [英] Django InlineFormSet validation
问题描述
我有一个 BaseInlineFormSet
,我想根据子域的值来验证父表单中的一个域.如文档,进行自定义验证的唯一方法是 clean()
,但我找不到仅向子级添加错误到父级表单的方法.
I have a BaseInlineFormSet
, and I'd like to validate a field in the parent form based on the values on the fields of the children. As seen in the docs, the only method to make a custom validation is clean()
, but I can't find a way to add errors to the parent form, only for the children.
在以下代码中,我建立了一个公式.每个变量都来自内部表单,如果全局公式未通过验证,我想向父级的表单字段 formula
In the following code I build a formula. Each variable comes from a inner form, and if the global formula don't validate, I'd like to add an error to the parent's form field formula
class CustomInlineFormSet(BaseInlineFormSet):
def clean(self):
formula = self.instance.formula
variables = []
for form in self.forms:
if 'formula_variable' in form.cleaned_data:
variables.append(form.cleaned_data['formula_variable'])
valid, msg = validate_formula(formula, variables)
if not valid:
WHAT HERE???
推荐答案
感谢 Russell Keith-Magee ,我设法解决这个问题.我将在此处详细说明答案,以供将来参考.
Thanks to Russell Keith-Magee, I managed to solve this. I detail the answer here for future reference.
我做错了,InlineFormSet不是执行所需验证的正确位置,应该在父ModelForm中完成.但是父ModelForm无权访问子FormSet,因此我只需要将FormSet作为参数传递给ModelForm.所以事情现在看起来像这样:
I was doing it wrong, the InlineFormSet was not the right place to do the kind of validation I needed, it should be done in the parent ModelForm. But the parent ModelForm don't have access to the child FormSet, so I just need to pass the FormSet to the ModelForm as a parameter. So the thing looks like this now:
视图:
class FormulaCreate(CreateView):
model = Formula
template_name = "whatever.html"
def get_context_data(self, **kwargs):
context = super(ParameterRatioCreate, self).get_context_data(**kwargs)
if self.request.POST:
formset = FormulaVariablesFormSet(self.request.POST, instance=self.object)
context['form'] = FormulaForm(formset=formset, data=self.request.POST, instance=self.object)
context['formset'] = formset
else:
formset = FormulaVariablesFormSet(instance=self.object)
context['form'] = FormulaForm(formset=formset, instance=self.object)
context['formset'] = formset
return context
def form_valid(self, form):
context = self.get_context_data()
form = context['form']
formset = context['formset']
if form.is_valid() and formset.is_valid():
ratio = form.save()
formset = FormulaVariablesFormSet(self.request.POST, instance=ratio)
if formset.is_valid():
formset.save()
self.object = ratio
return redirect(self.get_success_url())
context['formset'] = formset
return self.render_to_response(context)
表格:
class FormulaForm(forms.ModelForm):
class Meta:
model = Formula
def __init__(self, formset, *args, **kwargs):
self.formset = formset
super(FormulaForm, self).__init__(*args, **kwargs)
def clean(self):
variables = {}
for form in self.formset:
if form.is_valid() and 'formula_variable' in form.cleaned_data:
variables[form.cleaned_data['formula_variable']] = 1
if 'formula' in self.cleaned_data:
formula = self.cleaned_data['formula']
valid, msg = do_formula(formula, variables)
if not valid:
self._errors['formula'] = self.error_class([msg])
del self.cleaned_data['formula']
return self.cleaned_data
FormSet:
FormulaVariablesFormSet = inlineformset_factory(Formula,
FormulaVariable,
extra=1)
这篇关于Django InlineFormSet验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!