Dynamic select options not validating Django

。_饼干妹妹 提交于 2019-12-11 09:46:42

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/19568936/dynamic-select-options-not-validating-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!