carltongibson / django-filter

A generic system for filtering Django QuerySets based on user selections

Home Page:https://django-filter.readthedocs.io/en/main/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Empty value for csv filter

folt opened this issue · comments

My class filter

class CompanyFilter(filters.FilterSet):
    brand_ids = filters.BaseInFilter(label=gettext('Brand ids'), field_name='company__brand')

When I make a request with a get parameter v1/company/?brand_ids= , I get the value [''] in the list.

This behavior has been fixed for the widget #684 , I think it is necessary to edit the logic in BaseCSVFilter.

You need to mixin with another Filter class. From the docs:

It is expected that this filter class is used in conjunction with another filter class...

Can you provide a failing example with the correct usage?

It took a lot longer than I thought. Using the class according to the documentation solved only a part of the problems.

my code

class NumberInFilter(filters.BaseInFilter, filters.NumberFilter):
    pass

class ClientFilter(filters.FilterSet):
    id__in = NumberInFilter(field_name='id', lookup_expr='in')

    class Meta:
        model = user_models.Client
        fields = [
            'id__in',
        ]

class ClientViewSet(viewsets.ModelViewSet):
    queryset = user_models.Client.objects.all()
    serializer_class = staff_serializers.ClientSerializer
    filter_class = filters_serializers.ClientFilter

my url
http://127.0.0.1:8000/v1/user/client/?id__in=,,

Снимок экрана 2022-01-03 в 21 10 46

I propose to change the logic of the BaseCSVField.clean method, which should also check the constants.EMPTY_VALUES value and not include them in the filter sheet.