作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建了一个带有自定义验证的自定义表单,如下所示:
class MyCustomForm(forms.Form):
# ... form fields here
def clean(self):
cleaned_data = self.cleaned_data
# ... do some cross-fields validation here
return cleaned_data
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
cleaned_data
在我看来这有点奇怪。
最佳答案
你做得很好,但你应该像这样从 super 调用加载cleaned_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
关于Django 如何在自定义表单的子类中覆盖 clean() 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16231183/
我是一名优秀的程序员,十分优秀!