Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search without index #11

Open
pySilver opened this issue Aug 20, 2019 · 5 comments
Open

Search without index #11

pySilver opened this issue Aug 20, 2019 · 5 comments

Comments

@pySilver
Copy link

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

@gasman
Copy link
Collaborator

gasman commented Aug 21, 2019

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.

@cspollar
Copy link

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

@brylie
Copy link

brylie commented Jun 21, 2021

Thanks for the solution @cspollar

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

@enzedonline
Copy link

@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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants