Marek
Marek

Reputation: 1249

Django-filter Boolean Field with custom values

After successfully implementing crispy-forms and django-tables 2 it came for me to implement filtering in tables using django-filter and I am ripping my hair out.

First of all I find it much worse documented and probably this is why I can't find info that I need.
I would like to have a dropdown in the form with which I can select weather to show All users, Superusers, Normal users.
So I have moved from BooleanFilter to ChoiceFilter with custom choices.

class UserFilter(filters.FilterSet):
    SUPERUSER_CHOICES = {
        ('', 'All'),
        ('True', 'Superusers'),
        ('False', 'Normal users')
    }
    username = filters.CharFilter(field_name='username', lookup_expr='icontains')
    is_superuser = filters.ChoiceFilter(field_name='is_superuser', lookup_expr='exact',
                                        choices=SUPERUSER_CHOICES,)
    test = filters.Filter

    class Meta:
        model = User
        fields = ['username', 'is_superuser']

And this is what I get:

enter image description here

Why are these dashes there? How to get rid of them?
Maybe I don't even need to use ChoiceFilter to change values of options?

Upvotes: 2

Views: 2024

Answers (1)

YPCrumble
YPCrumble

Reputation: 28732

You need to set empty_label to None on the ChoiceFilter per the docs here:

is_superuser = filters.ChoiceFilter(
    field_name='is_superuser', 
    lookup_expr='exact', 
    choices=SUPERUSER_CHOICES, 
    empty_label=None
)

See also this answer to better understand Django's empty_label.

Upvotes: 1

Related Questions