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 anchor = models.CharField(max_length=64)
11 text = models.TextField()
12 search_vector = SearchVectorField()
16 GinIndex('search_vector', name='search_vector_idx'),
19 def save(self, *args, **kwargs):
20 super().save(*args, **kwargs)
21 if not self.search_vector:
25 self.search_vector = UnaccentSearchVector('text')
30 cls.objects.all().update(search_vector = build_search_vector('text'))