wagtail-generic-chooser icon indicating copy to clipboard operation
wagtail-generic-chooser copied to clipboard

Search without index

Open pySilver opened this issue 5 years ago • 5 comments

Why do we need an index for simple search functionality? Wouldn't it be easier to suppport simple contains by field(s) ?

pySilver avatar Aug 20 '19 21:08 pySilver

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.

gasman avatar Aug 21 '19 10:08 gasman

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', ]

cspollar avatar Apr 30 '20 18:04 cspollar

Thanks for the solution @cspollar

@gasman would this be introduced into the project, either as an available mixin or at least in the documentation?

brylie avatar Jun 21 '21 09:06 brylie

@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?

image

enzedonline avatar Mar 05 '23 19:03 enzedonline

Ahhh ... it's all in the placing of the "listing" element in the results template. Great solution, thanks again!

enzedonline avatar Mar 05 '23 19:03 enzedonline