Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django-filter How to change placeholder to replace [invalid name]?

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 ?

like image 987
Michel MARTIN Avatar asked Mar 06 '23 10:03

Michel MARTIN


1 Answers

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

This is how the filter will appear in the browseable API

like image 134
Angela Avatar answered Mar 31 '23 13:03

Angela