From 7c175736e9ac3a7fd0570cacfeda9ea6671e94ac Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Thu, 23 Jun 2016 13:52:21 +0100 Subject: [PATCH] Minor refactoring of must_call_distinct --- rest_framework/filters.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/rest_framework/filters.py b/rest_framework/filters.py index 4ac9429572..fdd9519c62 100644 --- a/rest_framework/filters.py +++ b/rest_framework/filters.py @@ -156,14 +156,15 @@ def construct_search(self, field_name): lookup = 'icontains' return LOOKUP_SEP.join([field_name, lookup]) - def must_call_distinct(self, opts, lookups): + def must_call_distinct(self, queryset, search_fields): """ Return True if 'distinct()' should be used to query the given lookups. """ - for lookup in lookups: - if lookup[0] in self.lookup_prefixes: - lookup = lookup[1:] - parts = lookup.split(LOOKUP_SEP) + opts = queryset.model._meta + for search_field in search_fields: + if search_field[0] in self.lookup_prefixes: + search_field = search_field[1:] + parts = search_field.split(LOOKUP_SEP) for part in parts: field = opts.get_field(part) if hasattr(field, 'get_path_info'): @@ -195,10 +196,11 @@ def filter_queryset(self, request, queryset, view): ] queryset = queryset.filter(reduce(operator.or_, queries)) - if self.must_call_distinct(queryset.model._meta, search_fields): + if self.must_call_distinct(queryset, search_fields): # Filtering against a many-to-many field requires us to # call queryset.distinct() in order to avoid duplicate items # in the resulting queryset. + # We try to avoid this is possible, for performance reasons. queryset = distinct(queryset, base) return queryset