Search code examples
djangodjango-filters

How to remove django-filter additional keywords in form fileld label


When i customize search type in the filter:

django_filters.CharFilter(lookup_expr='icontains')

it adds '[field_name] contains' keyword to the end of displaying label in template {{ field.label }}. I specifically use verbose_name='Some filed' argument in my model to make it accessible in template, but filter modifies it for inexplicable reason. Is there any option for it to be displayed as i set it in versbose_name?

UPD: Solved this adding this to settings.py

 def FILTERS_VERBOSE_LOOKUPS():
    from django_filters.conf import DEFAULTS

    verbose_lookups = DEFAULTS['VERBOSE_LOOKUPS'].copy()
    verbose_lookups.update({
        'exact': (''),
        'iexact': (''),
        'contains': (''),
        'icontains': (''),
    })
    return verbose_lookups

Solution

  • You can add the label parameter to the filter to explicitly set the field's label:

    django_filters.CharFilter(lookup_expr='icontains', label='the label')
    

    https://django-filter.readthedocs.io/en/master/ref/filters.html#label

    Also, if you don't like the way django-filters creates its filters, you can play with the FILTERS_VERBOSE_LOOKUPS setting (https://django-filter.readthedocs.io/en/master/ref/settings.html#verbose-lookups-setting). I haven't tested it but it says that you can set it to False to disable this behavior - you may want to try this instead.