Skip to content Skip to sidebar Skip to footer

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?

<inputtype="text"name="example"list="exampleList"><datalistid="exampleList"><option>hello</option><option>there</option></datalist>

if you do, you can check this.

classListTextWidget(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'})

    defrender(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)


classExampleForm(ModelForm)
    ...
    include = ['exampleOne', ...]
    widgets = { 
    'exampleOne': ListTextWidget(data_list=yourModel.objects.all(),name='exampleOne')
    }

Post a Comment for "Multivaluefield Does Not Work With Modelchoicefield"