use*_*018 11 django django-forms
我创建了一个自定义表单,如下所示:
class MyCustomForm(forms.Form):
# ... form fields here
def clean(self):
cleaned_data = self.cleaned_data
# ... do some cross-fields validation here
return cleaned_data
Run Code Online (Sandbox Code Playgroud)
现在,这个表单被子类化为另一个具有自己的清洁方法的表单.
触发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
Run Code Online (Sandbox Code Playgroud)
它似乎工作.但是,这会使两个clean()方法返回cleaned_data,这在我看来有点奇怪.
这是正确的方法吗?
Mou*_*nir 19
你做得很好,但你应该从超级调用加载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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16841 次 |
| 最近记录: |