python - MultiValueField does not work with ModelChoiceField -


the code: (where addressinput multiwidget)

class addressinput(widgets.multiwidget):     def __init__(self, attrs=none):         self.widgets = widgets.hiddeninput(attrs), widgets.textinput(attrs) # note second widget customized, i'm providing simplified example, produce same error         super().__init__(self.widgets, attrs)      def decompress(self, value):         try:             address = addresspoint.objects.get(pk=value)         except (addresspoint.doesnotexist, valueerror):             address = none          return [value, str(address)]      def value_from_datadict(self, data, files, name):         return tuple(widget.value_from_datadict(data, files, f'{name}_{i}') i, widget in enumerate(self.widgets))   class addressformfield(multivaluefield):     widget = addressinput      def __init__(self, queryset, empty_label="---------", to_field_name=none, limit_choices_to=none, *args, **kwargs):         fields = (             modelchoicefield(queryset, empty_label=empty_label, to_field_name=to_field_name, limit_choices_to=limit_choices_to, *args, **kwargs),             charfield()         )          super().__init__(fields=fields, require_all_fields=false, *args, **kwargs)          #self.widget.choices = self.fields[0].widget.choices  #### if not commented out, error: attributeerror: 'relatedfieldwidgetwrapper' object has no attribute 'decompress'      def compress(self, data_list):         if not data_list[1]:             return none          if not data_list[0]:             raise validationerror('invalid address')          return data_list[0]   class addressforeignkey(foreignkey):     def formfield(self, **kwargs):         # standard way set defaults         # while letting caller override them.         defaults = {'form_class': addressformfield}         defaults.update(kwargs)         return super().formfield(**defaults) 

i error: attributeerror: 'addressinput' object has no attribute 'choices' because modelchoicefield did not declare it. passing widget modelchoicefield not work makes copy if it's instance. set choices attribute manually can see in commented out code. got error didn't resolve: attributeerror: 'relatedfieldwidgetwrapper' object has no attribute 'decompress'

do want make widget in django?

<input type="text" name="example" list="examplelist"> <datalist id="examplelist">   <option>hello</option>   <option>there</option> </datalist> 

if do, 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         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')     } 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -