Django-filter如何更改占位符以重新设置[无效名称]?

时间:2018-04-12 08:12:58

标签: python django django-filters

我创建了一个过滤器来搜索多个字段(我必须只有一个搜索字段),如下所示:

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', ]

但是在html中,占位符显示了我的诡计!

<input type="text" name="full_search" class="form-control" placeholder="[invalid name]" title="" id="id_full_search">

是否可以通过其他方式进行更改?

2 个答案:

答案 0 :(得分:0)

最后我用JQuery做了这个:

$('#id_full_search').attr("placeholder", "Recherche")

答案 1 :(得分:0)

我知道这个问题已经问了一段时间,但只是解决了一个项目中的类似问题,因此将其发布在这里可以帮助其他人遇到这个问题。

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