from django.db.models.signals import post_save, post_delete
from django.db import models

from wagtail.wagtailsearch.index import Indexed
from wagtail.wagtailsearch.backends import get_search_backends


def post_save_signal_handler(instance, **kwargs):
    for backend in get_search_backends():
        backend.add(instance)


def post_delete_signal_handler(instance, **kwargs):
    for backend in get_search_backends():
        backend.delete(instance)


def register_signal_handlers():
    # Get list of models that should be indexed
    indexed_models = [model for model in models.get_models() if issubclass(model, Indexed)]

    # Loop through list and register signal handlers for each one
    for model in indexed_models:
        post_save.connect(post_save_signal_handler, sender=model)
        post_delete.connect(post_delete_signal_handler, sender=model)

from django.db.models.signals import post_save, post_delete
from django.db import models

from wagtail.wagtailsearch.index import Indexed
from wagtail.wagtailsearch.backends import get_search_backends


def post_save_signal_handler(instance, **kwargs):
    if instance not in type(instance).get_indexed_objects():
        return


    for backend in get_search_backends():
        backend.add(instance)


def post_delete_signal_handler(instance, **kwargs):
    if instance not in type(instance).get_indexed_objects():
        return

    for backend in get_search_backends():
        backend.delete(instance)


def register_signal_handlers():
    # Get list of models that should be indexed
    indexed_models = [model for model in models.get_models() if issubclass(model, Indexed)]

    # Loop through list and register signal handlers for each one
    for model in indexed_models:
        post_save.connect(post_save_signal_handler, sender=model)
        post_delete.connect(post_delete_signal_handler, sender=model)

Make search signal handlers use get_indexed_objects
Make search signal handlers use get_indexed_objects

  from django.db.models.signals import post_save, post_delete
  from django.db import models
  
  from wagtail.wagtailsearch.index import Indexed
  from wagtail.wagtailsearch.backends import get_search_backends
  
  
  def post_save_signal_handler(instance, **kwargs):
+     if instance not in type(instance).get_indexed_objects():
+         return
+ 
+ 
      for backend in get_search_backends():
          backend.add(instance)
  
  
  def post_delete_signal_handler(instance, **kwargs):
+     if instance not in type(instance).get_indexed_objects():
+         return
+ 
      for backend in get_search_backends():
          backend.delete(instance)
  
  
  def register_signal_handlers():
      # Get list of models that should be indexed
      indexed_models = [model for model in models.get_models() if issubclass(model, Indexed)]
  
      # Loop through list and register signal handlers for each one
      for model in indexed_models:
          post_save.connect(post_save_signal_handler, sender=model)
          post_delete.connect(post_delete_signal_handler, sender=model)
  
Make search signal handlers use get_indexed_objects
## Code Before:
from django.db.models.signals import post_save, post_delete
from django.db import models

from wagtail.wagtailsearch.index import Indexed
from wagtail.wagtailsearch.backends import get_search_backends


def post_save_signal_handler(instance, **kwargs):
    for backend in get_search_backends():
        backend.add(instance)


def post_delete_signal_handler(instance, **kwargs):
    for backend in get_search_backends():
        backend.delete(instance)


def register_signal_handlers():
    # Get list of models that should be indexed
    indexed_models = [model for model in models.get_models() if issubclass(model, Indexed)]

    # Loop through list and register signal handlers for each one
    for model in indexed_models:
        post_save.connect(post_save_signal_handler, sender=model)
        post_delete.connect(post_delete_signal_handler, sender=model)

## Instruction:
Make search signal handlers use get_indexed_objects
## Code After:
from django.db.models.signals import post_save, post_delete
from django.db import models

from wagtail.wagtailsearch.index import Indexed
from wagtail.wagtailsearch.backends import get_search_backends


def post_save_signal_handler(instance, **kwargs):
    if instance not in type(instance).get_indexed_objects():
        return


    for backend in get_search_backends():
        backend.add(instance)


def post_delete_signal_handler(instance, **kwargs):
    if instance not in type(instance).get_indexed_objects():
        return

    for backend in get_search_backends():
        backend.delete(instance)


def register_signal_handlers():
    # Get list of models that should be indexed
    indexed_models = [model for model in models.get_models() if issubclass(model, Indexed)]

    # Loop through list and register signal handlers for each one
    for model in indexed_models:
        post_save.connect(post_save_signal_handler, sender=model)
        post_delete.connect(post_delete_signal_handler, sender=model)
