Created
January 13, 2021 17:28
-
-
Save diek/0141ab66c426d52f3097441e2f5f236f to your computer and use it in GitHub Desktop.
Management Command to update search_vector field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.