Merge pull request #3103 from tomchristie/trollknurr-master

Ensure no duplication of results with SearchFilter with Oracle databases.
This commit is contained in:
Tom Christie 2015-07-02 12:23:31 +01:00
commit 21ec3b506a

View File

@ -7,6 +7,7 @@ from __future__ import unicode_literals
import operator
from functools import reduce
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.db import models
from django.utils import six
@ -59,6 +60,7 @@ class DjangoFilterBackend(BaseFilterBackend):
class Meta:
model = queryset.model
fields = filter_fields
return AutoFilterSet
return None
@ -100,13 +102,20 @@ class SearchFilter(BaseFilterBackend):
if not search_fields:
return queryset
original_queryset = queryset
orm_lookups = [self.construct_search(six.text_type(search_field))
for search_field in search_fields]
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)).distinct()
queryset = queryset.filter(reduce(operator.or_, or_queries))
if settings.DATABASES[queryset.db]["ENGINE"] == "django.db.backends.oracle":
# distinct analogue for Oracle users
queryset = original_queryset.filter(pk__in=set(queryset.values_list('pk', flat=True)))
else:
queryset = queryset.distinct()
return queryset