动态选择选项不验证Django [英] Dynamic select options not validating Django
本文介绍了动态选择选项不验证Django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
FooForm(modelform)
too = forms.ChoiceField(widget = forms.Select())
类Meta:
model = Foo
在模板中,我将动态添加选项到另外还有一个字段
$(''too')append('< option value ='timepass'>'+ timepass '< /选项>');
在视图中,这些值不能验证,因为没有可用的选择可供比较。如何验证并提取此字段?感谢提前。
解决方案
而不是使用 forms.ChoiceField
必须使用 forms.CharField
,与 widget = forms.Select()
,这样你可以接受任何价值,不受
格式的选择列表的限制
I have a model Foo. I have a modelform FooForm. In FooForm I am adding a dynamic field "too". This is a select field.
FooForm(modelform)
too = forms.ChoiceField(widget = forms.Select())
class Meta:
model = Foo
In the template I am adding options dynamically to this "too field"
$('.too').append('<option value='timepass'>'+timepass'</option>');
In the view these values are not validating, as there are no choices available to it for comparing. How do I validate and extract this field ? Thanks in advance.
解决方案
Instead of using a forms.ChoiceField
you must use forms.CharField
, with widget=forms.Select()
, this way you are open to any value and not limited by a choices list in the form
这篇关于动态选择选项不验证Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文