我使用自定义验证创建了一个自定义表单:
class MyCustomForm(forms.Form):
# ... form fields here
def clean(self):
cleaned_data = self.cleaned_data
# ... do some cross-fields validation here
return cleaned_data
现在,这个表单被子类化为另一个具有自己的清洁方法的表单
触发clean()方法的正确方法是什么?
目前,这就是我的工作:
class SubClassForm(MyCustomForm):
# ... additional form fields here
def clean(self):
cleaned_data = self.cleaned_data
# ... do some cross-fields validation for the subclass here
# Then call the clean() method of the super class
super(SubClassForm, self).clean()
# Finally, return the cleaned_data
return cleaned_data
似乎有效。但是,这使得两个clean()方法返回cleaned_data
,这在我看来有点奇怪
这是正确的方法吗?
答案 0 :(得分:17)
你做得很好,但你应该从超级电话中加载cleaning_data:
class SubClassForm(MyCustomForm):
# ... additional form fields here
def clean(self):
# Then call the clean() method of the super class
cleaned_data = super(SubClassForm, self).clean()
# ... do some cross-fields validation for the subclass
# Finally, return the cleaned_data
return cleaned_data