django_filters自定义方法字段名称

时间:2019-01-06 13:50:45

标签: django django-rest-framework django-filter

运行此代码时,我在django rest-framework视图的过滤器弹出窗口中得到 [无效名称]

我想显示名称“查找” 。 我该如何设置这个名字?

class MyModelFilter(filters.FilterSet):
    search_name = filters.CharFilter(field_name='name', lookup_expr='icontains')
    find_anywhere = filters.CharFilter(method='look_anywhere')

    def look_anywhere(self, queryset, name, value):
        return queryset.filter(Q(name__icontains=value) | Q(description__icontains=value))

    class Meta:
        model = MyModel
        fields = {'search', 'find_anywhere'}

致电

/api/mymodels/?find_anywhere=something

它工作正常。

它只是DRF视图中过滤器的名称:

enter image description here

1 个答案:

答案 0 :(得分:3)

search_name = filters.CharFilter(field_name='name', lookup_expr='icontains', label="Put Label Here")