yourlabs / django-autocomplete-light

A fresh approach to autocomplete implementations, specially for Django. Status: v4 alpha, v3 stable, v2 & v1 deprecated.
https://django-autocomplete-light.readthedocs.io
MIT License
1.8k stars 467 forks source link

Unexpected forward strategy for MultipleHiddenInput #1254

Open danieltellez opened 3 years ago

danieltellez commented 3 years ago

This is my code:

# forms.py
class MyForm(forms.ModelForm):
    multiple_field = forms.ModelMultipleChoiceField(widget=forms.MultipleHiddenInput(), queryset=whatever)

    class Meta:
        ...
        widgets = {
            'other_field': autocomplete.ModelSelect2(url='whatever-url', forward=['multiple_field', ]),
        }

# views.py
class MyAutocomplete(...):
    model = ...

    def get_queryset(self):
        qs = super().get_queryset()
        multiple_field = self.forwarded.get('multiple_field', None)
        ...

I expected to get a list of values on multiple_field variable on views.py. But it looks like in this context, getForwardStrategy returns single, so I get only the first value.

If I set the default widget for multiple_field, the behavior is correct. I've also tried adding Field('multiple_field', multiple='') on the form, but it didn't work either.

As a workaround, I'll hide my field with CSS. But, Is it possible to get the correct behaviour with some other configuration?

jpic commented 2 years ago

Does it work when you just return "multiple" here?

https://github.com/yourlabs/django-autocomplete-light/blob/master/src/dal/static/autocomplete_light/autocomplete_light.js#L293

danieltellez commented 2 years ago

Does it work when you just return "multiple" here?

Yes.

And it also works if I remove element.length === 1 on this line. Anyone knows why we need this first check ? It is not enough to check if the element has the "multiple" attribute to return "multiple"? Which is the case when we have "multiple" and the length of the element is "one" that we want to get a single element instead of a list?