Search code examples
djangodjango-filter

Django - set filter field label or verbose_name


I'm displaying a table of data using django-tables2.

For filtering I'm using the solution from here:

How do I filter tables with Django generic views?

My problem is only that I can't set the labels for the filter form. This is also imposible to google as words "django, form, filter, label" are quite general :(

My filter class:

import django_filters as filters
from models import Sale

class SaleFilter(filters.FilterSet):
    class Meta:
        model = Sale
        fields = ['CompanyProductID', 'CompanySellerID', 'CompanyRegisterID']
        labels = {
            'CompanyProductID': 'Article',
            'CompanySellerID': 'Seller',
            'CompanyRegisterID': 'Cash register'
        }     #THIS IS NOT WORKING

Solution

  • class ProductFilter(django_filters.FilterSet):
        class Meta:
            model = Product
            fields = ['manufacturer']
    
        def __init__(self, *args, **kwargs):
            super(ProductFilter, self).__init__(*args, **kwargs)
            self.filters['manufacturer'].extra.update(
                {'empty_label': 'All Manufacturers'})