MultiValueField Does Not Work With ModelChoiceField
The code: (where AddressInput is a MultiWidget) class AddressInput(widgets.MultiWidget): def __init__(self, attrs=None): self.widgets = widgets.HiddenInput(attrs), widg
Solution 1:
Do you want to make this as Widget in Django?
<input type="text" name="example" list="exampleList">
<datalist id="exampleList">
<option>hello</option>
<option>there</option>
</datalist>
if you do, you can check this.
class ListTextWidget(forms.TextInput):
def __init__(self, data_list, name, *args, **kwargs):
super().__init__(*args, **kwargs)
self._name = name
self._list = data_list
self.attrs.update({'list':'list_%s' % self._name, 'autocomplete':'off'})
def render(self, name, value, attrs=None):
text_html = super().render(name, value, attrs=attrs)
data_list = '<datalist id="list_%s">' % self._name
for item in self._list:
data_list += '<option value="%s">' % item
data_list += '</datalist>'
return (text_html + data_list)
class ExampleForm(ModelForm)
...
include = ['exampleOne', ...]
widgets = {
'exampleOne': ListTextWidget(data_list=yourModel.objects.all(),name='exampleOne')
}
Post a Comment for "MultiValueField Does Not Work With ModelChoiceField"