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

Multifield filtering on related models does not work as expected #1077

Open
dmugtasimov opened this issue May 9, 2019 · 2 comments
Open

Comments

@dmugtasimov
Copy link

dmugtasimov commented May 9, 2019

For this filterset:

class UserJobFilterSet(filters.FilterSet):
    field1 = filters.CharFilter('users_statuses__field1')
    field2 = filters.CharFilter('users_statuses__field1')

If both field1 and field2 provided it will not work as expected (AND operation) for reasons described here https://docs.djangoproject.com/en/2.2/topics/db/queries/#spanning-multi-valued-relationships and here https://stackoverflow.com/questions/8164675/chaining-multiple-filter-in-django-is-this-a-bug/8164920

To fix it this method:

    def filter_queryset(self, queryset):
        for name, value in self.form.cleaned_data.items():
            queryset = self.filters[name].filter(queryset, value)
        return queryset

should be reimplemented into collecting kwargs and submitting them in a single filter() call instead of nesting filter() calls.

@rpkilby
Copy link
Collaborator

rpkilby commented May 9, 2019

#745 is related. The short answer is that collecting kwargs or Q-objects isn't a workable solution, since it would break many types of filters. e.g., the OrderingFilter or any filter that should exclude instead of filter.

One workaround is to call qs._next_is_sticky() in the loop.

@rubbish822
Copy link

+1

@dmugtasimov dmugtasimov changed the title Multifield filtering on related models does work as expected Multifield filtering on related models does not work as expected May 10, 2019
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

3 participants