Option to choose search_fields for Select2MultipleWidget ?
sandeepbalagopal09 opened this issue · comments
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)
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
thanks @codingjoe it worked.