Clear any pre-existing cached entries on cache()

This commit is contained in:
jeremystretch 2022-10-12 09:07:37 -04:00
parent 607312a456
commit a237b2a10b

View File

@ -180,20 +180,27 @@ class CachedValueSearchBackend(SearchBackend):
@classmethod @classmethod
def cache(cls, instance, data): def cache(cls, instance, data):
ct = ContentType.objects.get_for_model(instance)
# Wipe out any previously cached values for the object
CachedValue.objects.filter(object_type=ct, object_id=instance.pk).delete()
# Record any new non-empty values
cached_values = []
for field in data: for field in data:
if not field.value: if not field.value:
continue continue
ct = ContentType.objects.get_for_model(instance) cached_values.append(
CachedValue.objects.update_or_create( CachedValue(
defaults={ object_type=ct,
'value': field.value, object_id=instance.pk,
'weight': field.weight, field=field.name,
}, type=field.type,
object_type=ct, weight=field.weight,
object_id=instance.pk, value=field.value
field=field.name, )
type=field.type
) )
CachedValue.objects.bulk_create(cached_values)
@classmethod @classmethod
def remove(cls, instance): def remove(cls, instance):