Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty value for csv filter #1398

Open
folt opened this issue Jul 13, 2021 · 2 comments
Open

Empty value for csv filter #1398

folt opened this issue Jul 13, 2021 · 2 comments

Comments

@folt
Copy link

folt commented Jul 13, 2021

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.

@carltongibson
Copy link
Owner

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?

@folt
Copy link
Author

folt commented Jan 3, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants