From fef95ac7651fb689e68c9bc01438eb851e0cc73c Mon Sep 17 00:00:00 2001 From: Matthew Farver Date: Thu, 22 Dec 2016 19:17:59 -0600 Subject: [PATCH] Check if an object is still in index_queryset There is an issue where if an object is updated in a way that removes it from the index_queryset it does not get removed upon update. This can be fixed by overriding update_object or instead should_update to check if an object is no longer in the queryset and removing instead of updating. --- celery_haystack/indexes.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/celery_haystack/indexes.py b/celery_haystack/indexes.py index 07dbf90..1e873ba 100644 --- a/celery_haystack/indexes.py +++ b/celery_haystack/indexes.py @@ -2,4 +2,12 @@ class CelerySearchIndex(indexes.SearchIndex): - pass + + def update_object(self, instance, using=None, **kwargs): + """Remove an object if it is no longer in the index_queryset""" + + ids = self.index_queryset(**kwargs).values_list('id', flat=True) + if instance.id not in ids: + self.remove_object(instance) + else: + super(ProjectIndex, self).update_object(instance, using, **kwargs)