Skip to content

Commit

Permalink
Add patch to Snippet chooser to customize search
Browse files Browse the repository at this point in the history
With this patch installed, the snippet chooser checks for a method
`chooser_search` in the model. If it exists, this method is used
instead of the search backend.
  • Loading branch information
donald committed Aug 14, 2020
1 parent 14616d4 commit f17efb5
Showing 1 changed file with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
From 7b829bfdcd151a256788b99276db0566983b1ee6 Mon Sep 17 00:00:00 2001
From: Donald Buczek <buczek@molgen.mpg.de>
Date: Fri, 14 Aug 2020 22:55:59 +0200
Subject: [PATCH] Chooser: Call search method from model if available

---
.../site-packages/wagtail/snippets/views/chooser.py | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/lib/python3.7/site-packages/wagtail/snippets/views/chooser.py b/lib/python3.7/site-packages/wagtail/snippets/views/chooser.py
index 7c93edd..26606cd 100644
--- a/lib/python3.7/site-packages/wagtail/snippets/views/chooser.py
+++ b/lib/python3.7/site-packages/wagtail/snippets/views/chooser.py
@@ -35,7 +35,10 @@ def choose(request, app_label, model_name):
search_query = search_form.cleaned_data['q']

search_backend = get_search_backend()
- items = search_backend.search(search_query, items)
+ if callable(getattr(model, "chooser_search", None)):
+ items = model.chooser_search(search_query)
+ else:
+ items = search_backend.search(search_query, items)
is_searching = True

else:
--
2.26.2

0 comments on commit f17efb5

Please sign in to comment.