【发布时间】:2022-01-19 08:52:19
【问题描述】:
我有一个包含两个字段的表单。应该要求用户只选择两者之一。不是两者都不是。 我尝试通过覆盖 Django Doc 中描述的 clean 方法来解决这个问题:
forms.py
class ConfigureWorkout(forms.Form):
first_exercise = forms.ModelChoiceField(empty_label="select", label="Choose First Exercise", queryset=FirstExercise.objects.all(), required=False)
sec_exercise = forms.ModelChoiceField(empty_label="select", label="Choose Sec Exercise", queryset=SecExercise.objects.all(), required=False)
def clean(self):
first_exercise = self.cleaned_data.get("first_exercise")
sec_exercise = self.cleaned_data.get("sec_exercise")
if first_exercise and sec_exercise:
raise forms.ValidationError("Enter either a First Exercise or a Secondary Exercise.")
else:
return self.cleaned_data
views.py
def configure(request):
configure_workout = ConfigureWorkout()
if request.method == "GET":
return render(request, "userprofile/some.html", configure_workout)
else:
return render(request, "app/other.html")
模板
<form action="{% url 'configure' %}" method="POST">
{% csrf_token %}
{{ configure_workout }}
<input type="submit" name="configuration_completed">
</form>
但是,如果我通过选择表单中的两个字段来对此进行测试,则不会显示/引发错误。我成功通过了表单并被发送到“other.html”。
我错过了什么? 非常感谢您的帮助:)
【问题讨论】:
-
您能否检查一下 clean 函数中返回的已清理数据(
first_exercise、sec_exercise)是否具有您对查询的期望值?以及这个数据有什么数据类型 -
此外,当覆盖
Form.clean()方法时,文档(Cleaning and validating fields that depend on each other) 声明应该调用super().clean()。 -
我尝试完全按照文档描述的方式进行操作(或者我希望如此),即使使用 '
super().clean()方法也是如此。行为不变。不太确定如何检查返回的清理数据?在 views.py 中调用print(configure_workout.clean())会引发AttributeError: 'ConfigureWorkout' object has no attribute 'cleaned_data'
标签: python django forms validation conditional-statements