2

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.