I create a filter to search in multiple fields (I must have only one search field) like this :
class myFilter(django_filters.FilterSet): full_search = django_filters.CharFilter(name='full_search', method='search_by_full_search')
def search_by_full_search(self, qs, name, value):
for term in value.split():
qs = qs.filter(Q(serial__icontains=term) | Q(id__icontains=term) | Q(name__icontains=term))
return qs
class Meta:
model = myModel
fields = ['full_search', ]
But in html, the placeholder shows my trickery !
<input type="text" name="full_search" class="form-control" placeholder="[invalid name]" title="" id="id_full_search">
Is it possible to change by something else ?
I know this has been asked a while ago, but just solved a similar problem in a project, so posting it here to help out if anyone else faces this issue.
full_search = django_filters.CharFilter(method='search_by_full_search', label='Recherche') # change label field to reflect what the filter name should be
class Meta:
model = myModel
fields = ['full_search', ]
def search_by_full_search(self, qs, name, value):
for term in value.split():
qs = qs.filter(Q(serial__icontains=term) | Q(id__icontains=term) |
Q(name__icontains=term))
return qs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With