checks.py 1.2 KB

1234567891011121314151617181920212223242526272829303132
  1. from django.core.checks import Warning, register
  2. @register("search")
  3. def page_search_fields_check(app_configs, **kwargs):
  4. """Checks each page model with search_fields to core fields are included"""
  5. from wagtail.models import Page, get_page_models
  6. page_models = get_page_models()
  7. errors = []
  8. for cls in page_models:
  9. # Don't check models where indexing has been explicitly disabled
  10. if not cls.search_fields:
  11. continue
  12. # Only checks an initial subset of fields as only need to check some are missing to show the warning
  13. if not all(field in cls.search_fields for field in Page.search_fields[:10]):
  14. errors.append(
  15. Warning(
  16. "Core Page fields missing in `search_fields`",
  17. hint=" ".join(
  18. [
  19. "Ensure that {} extends the Page model search fields",
  20. "`search_fields = Page.search_fields + [...]`",
  21. ]
  22. ).format(cls.__name__),
  23. obj=cls,
  24. id="wagtailsearch.W001",
  25. )
  26. )
  27. return errors