0

I have two fields inheriting from the Wagtail Page models, such as:

class ModelA(Page):
    custom_field = models.CharField(max_length=255)

    search_fields = Page.search_fields + [
        index.SearchField("custom_field")
    ]

class ModelB(Page):
    custom_field = models.CharField(max_length=255)

    search_fields = Page.search_fields + [
        index.SearchField("custom_field")
    ]

I am using the Wagtail database search backend, with Postgres as my DBMS:

WAGTAILSEARCH_BACKENDS = {
    "default": {
        "BACKEND": "wagtail.search.backends.database",
    }
}

I would like to make a query from the Page class that traverses the Page model to find results from both ModelA and ModelB:

search_results = Page.objects.filter(custom_field="some value")

However, The above example throws an error:

django.core.exceptions.FieldError: Cannot resolve keyword 'custom_field' into field.

How can I search for values in custom_field across multiple models inheriting from the Wagtail Page model?

Brylie Christopher Oxley
  • 1,684
  • 1
  • 17
  • 34
  • 1
    `Page.objects.filter(custom_field="some value")` is a plain Django database query that does not make use of the Wagtail search capabilities - you need a `search(...)` clause for that. – gasman Jan 28 '23 at 17:53

0 Answers0