From the tutorials in Django 2 by Example, I don't understand:
step (2): Why is `Count('tags')` **not** counting
the total number of tags possessed by that post?
This code:
# List of similar posts
post_tags_ids = post.tags.values_list('id', flat=True)
similar_posts = Post.published.filter(tags__in=post_tags_ids)\
.exclude(id=post.id)
similar_posts = similar_posts.annotate(same_tags=Count('tags'))\
.order_by('-same_tags','-publish')[:4]
does this:
- searches similar posts by looking at their common tags.
- uses Count aggregation function to generate a calculated field
same_tags. - orders the result by the number of shared tags in descending order etc...
I searched Taggit's API reference but it seems irrelevant.