wagtail-autocomplete icon indicating copy to clipboard operation
wagtail-autocomplete copied to clipboard

Use computed property for autocomplete search

Open brylie opened this issue 6 years ago • 5 comments

Is it possible to use a model property for the autocomplete_search_field? E.g. a property like full_name that concatenates two fields first_name and last_name?

brylie avatar Feb 06 '19 18:02 brylie

Unfortunately, because autocomplete_search_field is concatenated directly into a database query, computing it using python isn't possible. I'm open to considering implementations of this (maybe we can create a autocomplete_search(search: str) -> Queryset method that defaults to our current implementation but is overridable...

@brylie Can you tell me more about your use case?

harrislapiroff avatar Feb 25 '19 16:02 harrislapiroff

Well, we basically needed to search for authors for an article. The authors table had given_name and family_name, fields, with a full_name property. I ended up adding a database field full_name and populating the value in the save hook. This feature request was early in my troubleshooting :-)

brylie avatar Feb 25 '19 17:02 brylie

That makes sense—and that's also the sort of thing that could plausibly be solved in-database with annotations or something like them. Hmmmmmmm. 🤔

harrislapiroff avatar Feb 25 '19 18:02 harrislapiroff

I'm officially marking this as a desirable feature and will accept a PR that makes the autocomplete search method customizable. See discussion in #47 for more information.

harrislapiroff avatar May 17 '19 21:05 harrislapiroff

Hi guys. It seems to me that flexibility could be greatly improved by allowing a list of fields to be specified for searching rather than limiting it to a single field. You could put the query together like so (note: the query string can also be split to allow single-word matching on different fields):

https://github.com/wagtail/wagtail/blob/master/wagtail/contrib/modeladmin/helpers/search.py#L31

ababic avatar Nov 16 '19 10:11 ababic