Use alias instead of annotate for intermediary similarity values

This commit is contained in:
Jake Howard 2023-06-02 17:35:20 +01:00
parent 033b8be859
commit b7bcaeff63
Signed by: jake
GPG key ID: 57AFB45680EDD477

View file

@ -74,7 +74,7 @@ class BlogPostPage(BaseContentPage):
except BlogPostListPage.DoesNotExist: except BlogPostListPage.DoesNotExist:
return BlogPostPage.objects.none() return BlogPostPage.objects.none()
similar_posts = listing_pages.exclude(id=self.id).annotate( similar_posts = listing_pages.exclude(id=self.id).alias(
title_similarity=TrigramSimilarity("title", self.title), title_similarity=TrigramSimilarity("title", self.title),
# If this page has no subtitle, ignore it as part of similarity # If this page has no subtitle, ignore it as part of similarity
subtitle_similarity=TrigramSimilarity("subtitle", self.subtitle) subtitle_similarity=TrigramSimilarity("subtitle", self.subtitle)
@ -83,7 +83,7 @@ class BlogPostPage(BaseContentPage):
) )
page_tags = list(self.tags.values_list("id", flat=True)) page_tags = list(self.tags.values_list("id", flat=True))
similar_posts = similar_posts.annotate( similar_posts = similar_posts.alias(
# If this page has no tags, ignore it as part of similarity # If this page has no tags, ignore it as part of similarity
# NB: Cast to a float, because `COUNT` returns a `bigint`. # NB: Cast to a float, because `COUNT` returns a `bigint`.
tag_similarity=Cast( tag_similarity=Cast(