django admin search with multiple words
How about override self.query
so split()
doesn't work?
from django.contrib.admin.views.main import ChangeList
class UnsplitableUnicode(str):
"An object that behaves like a unicode string but cannot be split()"
def split(self, *args, **kwargs):
return [self]
class MultiWordSearchChangeList(ChangeList):
"Changelist that allows searches to contain spaces"
def get_query_set(self, request):
self.query = UnsplitableUnicode(self.query)
return super(MultiWordSearchChangeList, self).get_query_set(request)
class FooAdmin(admin.ModelAdmin):
def get_changelist(self, request, **kwargs):
return MultiWordSearchChangeList
As mentioned by Thai Tran it's a bit messy. Here is the section you would have to edit.
from django.contrib import admin
from django.contrib.admin.views.main import ChangeList
class CustomChangeList(ChangeList):
def get_query_set(self, request):
#Basically copy and paste in entire function and edit the piece copied in here.
if self.search_fields and self.query:
orm_lookups = [construct_search(str(search_field))
for search_field in self.search_fields]
for bit in self.query.split():
or_queries = [models.Q(**{orm_lookup: bit})
for orm_lookup in orm_lookups]
qs = qs.filter(reduce(operator.or_, or_queries))
if not use_distinct:
for search_spec in orm_lookups:
if lookup_needs_distinct(self.lookup_opts, search_spec):
use_distinct = True
break
class FooAdmin(admin.ModelAdmin):
def get_changelist(self, request, **kwargs):
return CustomChangeList
Speaking from experience, overriding ChangeList has caused problems down the road.