Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[1.6.x] Moved get_search_results from BaseModelAdmin to ModelAdmin.

Refs #15961.

Backport of 470a9bb from master.
  • Loading branch information...
commit 9053c6da5fcfe98c05527591f9fd785223eb354a 1 parent 01a9604
Loïc Bistuer loic authored timgraham committed
Showing with 33 additions and 29 deletions.
  1. +33 −29 django/contrib/admin/options.py
62 django/contrib/admin/options.py
View
@@ -264,34 +264,6 @@ def get_prepopulated_fields(self, request, obj=None):
"""
return self.prepopulated_fields
- def get_search_results(self, request, queryset, search_term):
- # Apply keyword searches.
- def construct_search(field_name):
- if field_name.startswith('^'):
- return "%s__istartswith" % field_name[1:]
- elif field_name.startswith('='):
- return "%s__iexact" % field_name[1:]
- elif field_name.startswith('@'):
- return "%s__search" % field_name[1:]
- else:
- return "%s__icontains" % field_name
-
- use_distinct = False
- if self.search_fields and search_term:
- orm_lookups = [construct_search(str(search_field))
- for search_field in self.search_fields]
- for bit in search_term.split():
- or_queries = [models.Q(**{orm_lookup: bit})
- for orm_lookup in orm_lookups]
- queryset = queryset.filter(reduce(operator.or_, or_queries))
- if not use_distinct:
- for search_spec in orm_lookups:
- if lookup_needs_distinct(self.opts, search_spec):
- use_distinct = True
- break
-
- return queryset, use_distinct
-
def get_queryset(self, request):
"""
Returns a QuerySet of all model instances that can be edited by the
@@ -767,11 +739,43 @@ def get_list_filter(self, request):
"""
return self.list_filter
+ def get_search_results(self, request, queryset, search_term):
+ """
+ Returns a tuple containing a queryset to implement the search,
+ and a boolean indicating if the results may contain duplicates.
+ """
+ # Apply keyword searches.
+ def construct_search(field_name):
+ if field_name.startswith('^'):
+ return "%s__istartswith" % field_name[1:]
+ elif field_name.startswith('='):
+ return "%s__iexact" % field_name[1:]
+ elif field_name.startswith('@'):
+ return "%s__search" % field_name[1:]
+ else:
+ return "%s__icontains" % field_name
+
+ use_distinct = False
+ search_fields = self.get_search_fields(request)
+ if search_fields and search_term:
+ orm_lookups = [construct_search(str(search_field))
+ for search_field in search_fields]
+ for bit in search_term.split():
+ or_queries = [models.Q(**{orm_lookup: bit})
+ for orm_lookup in orm_lookups]
+ queryset = queryset.filter(reduce(operator.or_, or_queries))
+ if not use_distinct:
+ for search_spec in orm_lookups:
+ if lookup_needs_distinct(self.opts, search_spec):
+ use_distinct = True
+ break
+
+ return queryset, use_distinct
+
def get_preserved_filters(self, request):
"""
Returns the preserved filters querystring.
"""
-
match = request.resolver_match
if self.preserve_filters and match:
opts = self.model._meta
Please sign in to comment.
Something went wrong with that request. Please try again.