本文介绍了有条件地在Django模型表单中只需要一个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
无论如何根据是否填写了相同表单的另一个字段,有条件地要求一个字段?
Anyway to make a field conditionally required based on whether or not another field in the same form has been filled out?
If field1 has no data, but field2 does
form is valid.
If field1 has no data and field2 has no data
form is invalid
不寻找任何javascript解决方案。我觉得应该用django形式解决,但不太确定如何最好地去做。
Not looking for any javascript solutions. I feel as it should be solved with django forms, but not quite sure how best to go about it.
推荐答案
覆盖.clean (self)方法,检查self.cleaned_data并提高ValidationError
Override .clean(self) method, check for self.cleaned_data and raise ValidationError
这篇关于有条件地在Django模型表单中只需要一个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!