from django.conf import settings
from django.utils import translation

from celery.task import task
from haystack import site

@task
def delayed_update(instance_pk, model):
    """ Only index stuff that is known to be public """
    translation.activate(settings.LANGUAGE_CODE)
    try:
        instance = model.published.get(pk=instance_pk)
    except (model.DoesNotExist, AttributeError):
        return
    site.update_object(instance)

@task
def delayed_remove(instance_pk, model):
    translation.activate(settings.LANGUAGE_CODE)
    try:
        instance = model.published.get(pk=instance_pk)
    except (model.DoesNotExist, AttributeError):
        return
    site.remove_object(instance)

from django.conf import settings
from django.utils import translation

from celery.task import task
from haystack import site

@task
def delayed_update(instance_pk, model):
    """ Only index stuff that is known to be public """
    translation.activate(settings.LANGUAGE_CODE)
    try:
        instance = model.published.get(pk=instance_pk)
    except (model.DoesNotExist, AttributeError):
        return
    site.update_object(instance)

@task
def delayed_remove(instance_pk, model):
    translation.activate(settings.LANGUAGE_CODE)
    # Fake an instance (real one is already gone from the DB)
    fake_instance = model()
    fake_instance.pk = instance_pk
    site.remove_object(fake_instance)

Rewrite delayed_remove task: instance is gone, so cannot retrieve. Fake instance with proper pk and let haystack remove it from index.
Rewrite delayed_remove task: instance is gone, so cannot retrieve. Fake instance with proper pk and let haystack remove it from index.

  from django.conf import settings
  from django.utils import translation
  
  from celery.task import task
  from haystack import site
  
  @task
  def delayed_update(instance_pk, model):
      """ Only index stuff that is known to be public """
      translation.activate(settings.LANGUAGE_CODE)
      try:
          instance = model.published.get(pk=instance_pk)
      except (model.DoesNotExist, AttributeError):
          return
      site.update_object(instance)
  
  @task
  def delayed_remove(instance_pk, model):
      translation.activate(settings.LANGUAGE_CODE)
+     # Fake an instance (real one is already gone from the DB)
+     fake_instance = model()
+     fake_instance.pk = instance_pk
-     try:
-         instance = model.published.get(pk=instance_pk)
-     except (model.DoesNotExist, AttributeError):
-         return
-     site.remove_object(instance)
+     site.remove_object(fake_instance)
  
Rewrite delayed_remove task: instance is gone, so cannot retrieve. Fake instance with proper pk and let haystack remove it from index.
## Code Before:
from django.conf import settings
from django.utils import translation

from celery.task import task
from haystack import site

@task
def delayed_update(instance_pk, model):
    """ Only index stuff that is known to be public """
    translation.activate(settings.LANGUAGE_CODE)
    try:
        instance = model.published.get(pk=instance_pk)
    except (model.DoesNotExist, AttributeError):
        return
    site.update_object(instance)

@task
def delayed_remove(instance_pk, model):
    translation.activate(settings.LANGUAGE_CODE)
    try:
        instance = model.published.get(pk=instance_pk)
    except (model.DoesNotExist, AttributeError):
        return
    site.remove_object(instance)

## Instruction:
Rewrite delayed_remove task: instance is gone, so cannot retrieve. Fake instance with proper pk and let haystack remove it from index.
## Code After:
from django.conf import settings
from django.utils import translation

from celery.task import task
from haystack import site

@task
def delayed_update(instance_pk, model):
    """ Only index stuff that is known to be public """
    translation.activate(settings.LANGUAGE_CODE)
    try:
        instance = model.published.get(pk=instance_pk)
    except (model.DoesNotExist, AttributeError):
        return
    site.update_object(instance)

@task
def delayed_remove(instance_pk, model):
    translation.activate(settings.LANGUAGE_CODE)
    # Fake an instance (real one is already gone from the DB)
    fake_instance = model()
    fake_instance.pk = instance_pk
    site.remove_object(fake_instance)
