Is it possible to persist a joined field in Djangos SearchVectorField?


Is it possible to persist a joined field in Djangos SearchVectorField?



Is it possible to persist a joined field with Djangos SearchVectorField for full text search?



For example:


class P(models.Model):
brand = models.ForeignKey(Brand, on_delete=models.CASCADE)
search_vector = SearchVectorField(null=True, blank=True)



code:


p = P.objects.get(id=1)
p.search_vector = SearchVector('brand__name')
p.save()



raises this exception:


FieldError: Joined field references are not permitted in this query



If this is not possible how can you increase the performance of joined annotated queries?




1 Answer
1



I found a workaround to your issue:


p = P.objects.annotate(brand_name=SearchVector('brand__name')).get(id=1)
p.search_vector = p.brand_name
p.save()



Update 2018-06-29



As reported in official documentation:



If you’re just updating a record and don’t need to do anything with the model object, the most efficient approach is to call update(), rather than loading the model object into memory.



Using update() also prevents a race condition wherein something might change in your database in the short period of time between loading the object and calling save().



Finally, realize that update() does an update at the SQL level and, thus, does not call any save() methods on your models, nor does it emit the pre_save or post_save signals (which are a consequence of calling Model.save()).



So in this case you can use this query to perform a single SQL query on the database:


from django.contrib.postgres.search import SearchVector
from django.db.models import F

P.objects.annotate(
brand_name=SearchVector('brand__name')
).filter(
id=1
).update(
search_vector=F('brand_name')
)





aren't you making a redundant db query with that .get(id=1)? I'm also looking for a solution to build a search vector from a foreign relationship.
– Nad
Jun 29 at 15:51


.get(id=1)





@Nad I've updated my answer with a solution that perform one query only on db, but read the documentation about the difference between save and update
– Paolo Melchiorre
Jun 29 at 16:49





thanks for this. My issue is I already have an instance of an model and for which I want to update the search_vector from a post_save signal. I posted a question here stackoverflow.com/questions/51105651/…
– Nad
Jun 30 at 8:36





found an answer of yours to this here! stackoverflow.com/questions/42679743/… Brilliant, thank you
– Nad
Jun 30 at 8:50






@nad I'm you've found an useful answer for your problem. If it works please vote for it.
– Paolo Melchiorre
2 days ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV