@@ -156,14 +156,15 @@ def construct_search(self, field_name):
156
156
lookup = 'icontains'
157
157
return LOOKUP_SEP .join ([field_name , lookup ])
158
158
159
- def must_call_distinct (self , opts , lookups ):
159
+ def must_call_distinct (self , queryset , search_fields ):
160
160
"""
161
161
Return True if 'distinct()' should be used to query the given lookups.
162
162
"""
163
- for lookup in lookups :
164
- if lookup [0 ] in self .lookup_prefixes :
165
- lookup = lookup [1 :]
166
- parts = lookup .split (LOOKUP_SEP )
163
+ opts = queryset .model ._meta
164
+ for search_field in search_fields :
165
+ if search_field [0 ] in self .lookup_prefixes :
166
+ search_field = search_field [1 :]
167
+ parts = search_field .split (LOOKUP_SEP )
167
168
for part in parts :
168
169
field = opts .get_field (part )
169
170
if hasattr (field , 'get_path_info' ):
@@ -195,10 +196,11 @@ def filter_queryset(self, request, queryset, view):
195
196
]
196
197
queryset = queryset .filter (reduce (operator .or_ , queries ))
197
198
198
- if self .must_call_distinct (queryset . model . _meta , search_fields ):
199
+ if self .must_call_distinct (queryset , search_fields ):
199
200
# Filtering against a many-to-many field requires us to
200
201
# call queryset.distinct() in order to avoid duplicate items
201
202
# in the resulting queryset.
203
+ # We try to avoid this is possible, for performance reasons.
202
204
queryset = distinct (queryset , base )
203
205
return queryset
204
206
0 commit comments