redhaushq
redhaushq

Reputation: 155

wagtail main search add modelAdmin models

Quick question on wagtail's main search at the top of the left sidebar beneath the logo. By default that search box searches pages, images, documents, users.

Two questions: Is there a way to modify that search scope so it also includes modelAdmin models? Is there a way to remove pages from the search query list so it only searches images, documents, users?

I can't seem to find anything in the docs about it. I know you can search modelAdmin models once on the model admin list view, I have that working. I was just looking for a way to extend that search to be included on the main sidebar search as well.

Any direction you can provide would be much appreciated.

Upvotes: 0

Views: 1403

Answers (1)

LB Ben Johnston
LB Ben Johnston

Reputation: 5196

The admin search area does show multiple items, such as pages, images, documents etc.

However, this page only searches page models, when you click the other models (e.g. images), it will take you to the images search page, it will also append the query param q based on any existing search.

So just to clarify, this page only searches pages and to search for other items, it takes you elsewhere in the Wagtail admin.

Hopefully the below answers your specific scenario questions.

1. Modify the admin/pages search scope or results

  • This could possibly be done by making your own view, but it is not simple
  • You can see the search view function here https://github.com/wagtail/wagtail/blob/master/wagtail/admin/views/pages.py#L958
  • You can redirect any URL by modifying your urls.py to direct the admin/pages/search/ page to your custom view.
  • However, you would likely need to re-write (copy/paste) most of the view as it is a function, not class view

2. Adding custom search areas

3. Removing a search area

  • The simplest way to hide one of the items in the 'other searches' list would be with a CSS change
  • The next best way would be to customise the template wagtail/admin/templates/wagtailadmin/shared/search_other.html with an override and then filter the results
  • I am not aware of a way to remove hooks or registered hooks without some monkey patching, but the hooks get collected and stored as admin_search_areas you can see the code here - https://github.com/wagtail/wagtail/blob/master/wagtail/admin/search.py#L102

Example Code for using the register_admin_search_area hook

This basically uses the documentation example but gives you a rough idea of how to go to a specific ModelAdmin index view using this feature. Note: this does not search ALL ModelAdmin models, only one specific one.

You can always make a custom search page for searching all models at once though.

from wagtail.core import hooks
from wagtail.admin.search import SearchArea

# ...

@hooks.register('register_admin_search_area')
def register_model_admin_search_area():
    # PeopleModelAdmin is a ModelAdmin also in the same hooks file
    index_url = PeopleModelAdmin().url_helper.index_url
    return SearchArea(
        'People ModelAdmin',
        index_url,
        classnames='icon icon-user',
        order=10000
    )


@hooks.register('register_admin_search_area')
def register_snippets_search_area():
    url = reverse('wagtailsnippets:list', args=('base', 'people'))
    return SearchArea(
        'People Snippets',
        url,
        classnames='icon icon-user',
        order=10000
    )

Upvotes: 2

Related Questions