Skip to content

Instantly share code, notes, and snippets.

@diek
Created January 13, 2021 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diek/0141ab66c426d52f3097441e2f5f236f to your computer and use it in GitHub Desktop.
Save diek/0141ab66c426d52f3097441e2f5f236f to your computer and use it in GitHub Desktop.
Management Command to update search_vector field
from django.contrib.postgres.search import SearchVector
from django.core.management import BaseCommand
from myapp.models import Article
class Command(BaseCommand):
# Provide help at command prompt for user
help = "Add search vector articles"
# A command must define handle()
def handle(self, *args, **options):
Article.objects.all().update(
search_vector=(
SearchVector("headline", weight="A")
+ SearchVector("content", weight="B")
)
)
self.stdout.write("Complete")
@jsagoe1
Copy link

jsagoe1 commented Mar 4, 2021

Hi,
I saw your comment on this article. https://www.netlandish.com/blog/2020/06/22/full-text-search-django-postgresql/
I am trying to implement the same thing in my django app and I had to change my save function to this
https://github.com/jsagoe1/Python_Stuff/blob/master/models_temp.py

I read in another article that the model instance has to be created before the search vector member can be updated.
Please let me know what you think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment