I know how to do this with raw PostgreSQL commands, but want to know if there is a way to do this with Django PostgreSQL search.
class Person(models.Model):
name = models.CharField(max_length=64)
description = models.CharField(max_length=256)
active = models.BooleanField(default=False)
search_vector = SearchVectorField(blank=True)
def update_search(person):
vector = SearchVector('name') + SearchVector('description')
if person.active:
vector = vector + SearchVector('alive')
person.search_vector = vector
django.core.exceptions.FieldError: Cannot resolve keyword 'alive' into field.
I tried making 'alive' a @property method, but it looks like it only wants a db field for search.
Is there a way to do this in pure Django ORM or should I go the raw SQL route?
You can use Value() expressions to add a string to in your SearchVector as in below example:
from django.db.models import Value
...
vector = vector + SearchVector(Value('alive'))
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With