问题
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