working proof of concept
This commit is contained in:
25
cookbook/signals.py
Normal file
25
cookbook/signals.py
Normal file
@ -0,0 +1,25 @@
|
||||
from django.contrib.postgres.search import SearchVector
|
||||
from django.db.models.signals import post_save
|
||||
from django.dispatch import receiver
|
||||
|
||||
from cookbook.models import Recipe
|
||||
|
||||
|
||||
@receiver(post_save, sender=Recipe)
|
||||
def update_recipe_search_vector(sender, instance=None, created=False, **kwargs):
|
||||
if not instance:
|
||||
return
|
||||
|
||||
if hasattr(instance, '_dirty'):
|
||||
return
|
||||
|
||||
instance.search_vector = (
|
||||
SearchVector('name', weight='A', config='english')
|
||||
+ SearchVector('description', weight='B', config='english')
|
||||
)
|
||||
|
||||
try:
|
||||
instance._dirty = True
|
||||
instance.save()
|
||||
finally:
|
||||
del instance._dirty
|
Reference in New Issue
Block a user