| 52 | |
| 53 | |
| 54 | Here is my usage: |
| 55 | |
| 56 | {{{ |
| 57 | class Document(models.Model): |
| 58 | title = models.CharField(max_length=255) |
| 59 | metadata = models.JSONField(null=True) |
| 60 | uploaded_at = models.DateTimeField(auto_now_add=True) |
| 61 | tags = models.ManyToManyField(Tag, related_name='documents') |
| 62 | type_fields = models.JSONField(default=dict) |
| 63 | search_vector = SearchVectorField(null=True) |
| 64 | |
| 65 | def save(self, *args, **kwargs): |
| 66 | super().save(*args, **kwargs) # Save the instance first |
| 67 | self.update_search_vector() |
| 68 | |
| 69 | def update_search_vector(self): |
| 70 | self.search_vector = SearchVector('title', 'metadata') |
| 71 | |
| 72 | Document.objects.filter(pk=self.pk).update(search_vector=self.search_vector) |
| 73 | |
| 74 | class Meta: |
| 75 | indexes = [ |
| 76 | GinIndex(SearchVector('search_vector'), name='Document search'), |
| 77 | ] |
| 78 | |
| 79 | }}} |