From 872eb672118ed6e4febdfef52d097afcc62caaf2 Mon Sep 17 00:00:00 2001 From: Oscar Cortez Date: Thu, 30 Apr 2020 18:26:32 -0600 Subject: [PATCH] Add search vector method --- src/wiki/models/article.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/wiki/models/article.py b/src/wiki/models/article.py index b5815454..beee6ad9 100644 --- a/src/wiki/models/article.py +++ b/src/wiki/models/article.py @@ -1,9 +1,10 @@ from django.conf import settings as django_settings from django.contrib.contenttypes.fields import GenericForeignKey from django.contrib.contenttypes.models import ContentType -from django.contrib.postgres.search import SearchVectorField +from django.contrib.postgres.search import SearchVector, SearchVectorField from django.core.cache import cache -from django.db import models +from django.db import models, transaction +from django.db.models import OuterRef, Subquery from django.db.models.fields import GenericIPAddressField as IPAddressField from django.db.models.signals import post_save, pre_delete, pre_save from django.urls import reverse @@ -102,6 +103,23 @@ class Article(models.Model): for obj in self.articleforobject_set.filter(is_mptt=True): yield from obj.content_object.get_descendants() + @transaction.atomic + def update_search_vector(self): + Article.objects.annotate( + wiki_title=Subquery( + Article.objects.filter(id=OuterRef('id')).values( + 'current_revision__title' + ) + ), + wiki_body=Subquery( + Article.objects.filter(id=OuterRef('id')).values( + 'current_revision__content' + ) + ), + ).filter(id=self.id).update( + search_vector=SearchVector('wiki_title', 'wiki_body') + ) + def get_children(self, max_num=None, user_can_read=None, **kwargs): """NB! This generator is expensive, so use it with care!!""" cnt = 0 -- 2.45.2