Changes between Initial Version and Version 1 of Ticket #35539, comment 4


Ignore:
Timestamp:
Jul 9, 2024, 12:17:27 AM (6 months ago)
Author:
Alastair D'Silva

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #35539, comment 4

    initial v1  
    5050
    5151}}}
     52
     53
     54Here is my usage:
     55
     56{{{
     57class 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}}}
Back to Top