applegrew / django-select2

This is a Django integration for Select2
MIT License
710 stars 316 forks source link

Option to choose search_fields for Select2MultipleWidget ? #582

Closed sandeepbalagopal09 closed 4 years ago

sandeepbalagopal09 commented 4 years ago

Goal I am using Select2MultipleWidget, one thing is fine except my client want to search based on istartswith query. Currently the search works by the __str__ of the object it seems. I want to change it to field_name___istartswith.

Code Snippet

class SearchArticleForm(forms.ModelForm):
    categories = forms.ModelMultipleChoiceField(queryset=ArticleCategory.objects.all(),
    widget=Select2MultipleWidget)
codingjoe commented 4 years ago

Hi @sandeepimpressads,

thanks for reaching out. If I understand you correctly, you will need to set the search_fields. I added them to your code example, see below:

class SearchArticleForm(forms.ModelForm):
    categories = forms.ModelMultipleChoiceField(
        queryset=ArticleCategory.objects.all(),
        widget=Select2MultipleWidget(search_fields=['field_name___istartswith']),
    )

Best -Joe

sandeepbalagopal09 commented 4 years ago

thanks @codingjoe it worked.