wagtail-generic-chooser
wagtail-generic-chooser copied to clipboard
Search without index
Why do we need an index for simple search functionality? Wouldn't it be easier to suppport simple contains
by field(s)
?
The only real reason is that I was replicating the functionality of the built-in choosers, where the search box always uses Wagtail search backends. Supporting SQL-based search as a fallback would be a worthwhile addition.
Subclassing ModelChooserMixin
allowed me to add search to a third party model. It's not quite as nice as built in support, but it seems to be a valid workaround for now.
# views.py
from generic_chooser.views import ModelChooserViewSet, ModelChooserMixin
from taggit.models import Tag
class TagChooserMixin(ModelChooserMixin):
"""Custom Mixin to enable searching of non-indexed taggit.Tag model."""
@property
def is_searchable(self):
return True
def get_object_list(self, search_term=None, **kwargs):
object_list = self.get_unfiltered_object_list()
if search_term:
object_list = object_list.filter(name__icontains=search_term)
return object_list
class TagChooserViewSet(ModelChooserViewSet):
chooser_mixin_class = TagChooserMixin
icon = 'tag'
model = Tag
page_title = "Choose a Tag"
per_page = 10
order_by = 'name'
fields = ['name', ]
Thanks for the solution @cspollar
@gasman would this be introduced into the project, either as an available mixin or at least in the documentation?
@cspollar - thanks for that, works great
I do get a 2nd search pagination message going on inside the main pagination though. Do you see this?
Ahhh ... it's all in the placing of the "listing" element in the results template. Great solution, thanks again!