diff --git a/rest_framework/filters.py b/rest_framework/filters.py index 743469f8f..1ef4c7fa0 100644 --- a/rest_framework/filters.py +++ b/rest_framework/filters.py @@ -101,18 +101,17 @@ class SearchFilter(BaseFilterBackend): orm_lookups = [self.construct_search(six.text_type(search_field)) for search_field in search_fields] - + and_queries = [] for search_term in self.get_search_terms(request): or_queries = [models.Q(**{orm_lookup: search_term}) for orm_lookup in orm_lookups] - queryset = queryset.filter(reduce(operator.or_, or_queries)) + and_queries.append(reduce(operator.or_, or_queries)) - if settings.DATABASES[queryset.db]["ENGINE"] != "django.db.backends.oracle": - queryset = queryset.distinct() + if settings.DATABASES[queryset.db]["ENGINE"] == "django.db.backends.oracle": + pk_list = queryset.filter(reduce(operator.and_, and_queries)).values_list('pk', flat=True) + return queryset.filter(pk__in=frozenset(pk_list)) else: - pk_list = queryset.values_list('pk', flat=True) - queryset = view.model.objects.filter(pk__in=set(pk_list)) - return queryset + return queryset.filter(reduce(operator.and_, and_queries)).distinct() class OrderingFilter(BaseFilterBackend):