1 from django.db import models
2 from django.contrib.postgres.indexes import GinIndex
3 from django.contrib.postgres.search import SearchVector, SearchVectorField
4 from search.utils import UnaccentSearchVector
7 class Snippet(models.Model):
8 book = models.ForeignKey('Book', models.CASCADE)
9 sec = models.IntegerField()
10 text = models.TextField()
11 search_vector = SearchVectorField()
15 GinIndex('search_vector', name='search_vector_idx'),
18 def save(self, *args, **kwargs):
19 super().save(*args, **kwargs)
20 if not self.search_vector:
24 self.search_vector = UnaccentSearchVector('text')
29 cls.objects.all().update(search_vector = build_search_vector('text'))