Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django_filters custom method field name

When I run this code I get [invalid name]: in the filters popup in django rest-framework views.

I would like to display the name "find" instead. How can I set this name?

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'}

when calling

/api/mymodels/?find_anywhere=something

it works fine.

It is only the name of the filter in the DRF views:

enter image description here

like image 284
Vlad Otrocol Avatar asked Dec 31 '25 01:12

Vlad Otrocol


1 Answers

search_name = filters.CharFilter(field_name='name', lookup_expr='icontains', label="Put Label Here")
like image 70
Roman Grigorov Avatar answered Jan 01 '26 16:01

Roman Grigorov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!